contentful-migration icon indicating copy to clipboard operation
contentful-migration copied to clipboard

Validation error - Invalid Regular Expression

Open FreshRob opened this issue 4 years ago • 2 comments

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

FreshRob avatar Sep 17 '21 11:09 FreshRob

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

dustinbrownman avatar Feb 10 '22 17:02 dustinbrownman

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+))+))$"

conorriches avatar Jan 05 '23 14:01 conorriches