Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation error - Invalid Regular Expression #896

Open
FreshRob opened this issue Sep 17, 2021 · 2 comments
Open

Validation error - Invalid Regular Expression #896

FreshRob opened this issue Sep 17, 2021 · 2 comments

Comments

@FreshRob
Copy link

I am using the same slug regex as Compose and I am able to use it in the contentful UI however the migration is rejecting it.

^((\/)|(([\/\w\-\._~:!$&'\(\)*+,;@]|(%\d+))+))$

Expected Behavior

Accept regex and create contentType

Actual Behavior

 Error: {"status":"Unprocessable Entity","message":"Validation error","details":{"errors":[{"name":"type","type":"Validation","value":{"regexp":{"pattern":"^((/)|(([/w-._~:!$&'()*+,;@]|(%d+))+))$"}},"details":"Invalid regular expression","path":["fields",1,"validations",0]}]},"url":"spaces/*****/environments/master/content_types/Article"}

Steps to Reproduce

  contentType
  .createField("slug")
  .name("Slug")
  .type("Symbol")
  .required(true)
  .validations([{ regexp: { pattern: "^((\/)|(([\/\w\-\._~:!$&'\(\)*+,;@]|(%\d+))+))$"  } }]);

Environment

master

  • Node Version: v14.10.1
  • Package Manager Version: 6.14.8
  • Operating System: <Windows 10 64bit
  • Package Version: 1.9.20
@dustinbrownman
Copy link

I'm running in to this same version on 1.10.0 of the Contentful CLI

@conorriches
Copy link

conorriches commented Jan 5, 2023

For anyone in the future reading this, I had this error too and fixed it:

slug_validation = Contentful::Management::Validation.new
slug_validation.regexp = {
  pattern: %r{^((\/)|(([\/\w\-\._~:!$&'\(\)*+,;@]|(%\d+))+))$}.inspect.gsub(%r{^/|/$}, '')
}
slug_validation.message = "That doesn't look like a valid slug"

namely by adding .inspect.gsub(%r{^/|/$}, '') got it to work for me, you can see below the difference namely with escape characters:

p = %r{^((\/)|(([\/\w\-\._~:!$&'\(\)*+,;@]|(%\d+))+))$}

p.inspect 
=> "/^((\\/)|(([\\/\\w\\-\\._~:!$&'\\(\\)*+,;@]|(%\\d+))+))$/"

p.inspect.gsub(%r{^/|/$}, '')
=> "^((\\/)|(([\\/\\w\\-\\._~:!$&'\\(\\)*+,;@]|(%\\d+))+))$"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants