Skip to content

Could you support incasesensitive pattern matching? #352

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

Closed
markomanninen opened this issue Jan 19, 2017 · 2 comments
Closed

Could you support incasesensitive pattern matching? #352

markomanninen opened this issue Jan 19, 2017 · 2 comments

Comments

@markomanninen
Copy link

markomanninen commented Jan 19, 2017

Schema:

{
  "firstname": {
    "type": "string",
    "pattern": "alan",
    "inCaseSensitive": true
  }
}

would use -i modifier on preg_match and match with Alan, ALAN, aLaN, etc.

markomanninen@d1011e3#diff-9129e81a8f8053dcd8747c5b88dbaef4

@shmax
Copy link
Collaborator

shmax commented Jan 29, 2017

@markomanninen

I think you're out of luck; according to the Json Schema specification, pattern SHOULD be valid according to the ECMA 262 [ecma262] regular expression dialect, and ECMA 262 doesn't allow for inline flag specifiers (and your idea of adding inCaseSensitive is a non-starter, as it also violates the spec).

That said, there is certainly nothing stopping you from extending StringConstraint and overriding check to taste:

class MyStringConstraint extends StringConstraint
{
    /**
     * {@inheritDoc}
     */
    public function check($element, $schema = null, JsonPointer $path = null, $i = null)
    {
        // Verify a regex pattern
        if (isset($schema->pattern) && !preg_match('#' . str_replace('#', '\\#', $schema->pattern) . '#ui', $element)) {
            $this->addError($path, "Does not match the regex pattern " . $schema->pattern, 'pattern', array(
                'pattern' => $schema->pattern,
            ));
        }
       else {
           parent::check($element, $schema, $path, $i);
       }
        
    }

Admittedly that's probably more duplicated code than needs to happen; it might be nice if the base StringConstraint class had a more-easily override-able helper method for constructing the final pattern string. If you wanted to open a PR that adds that I would support it.

Hope that helps (sorry for the long delay).

@DannyvdSluijs
Copy link
Collaborator

@markomanninen in an attempt to cleanup this repo we are trying to filter the issues and see which ones might be closed. Is it safe to assume this is a rather old issue, which sadly was left unanswered, and can be closed? Feel free to close it yourself with some comments if helpful.

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

4 participants