Hello! For validation json use library

https://github.com/justinrainbow/json-schema .

Made the following scheme:

 { "id": "http://json-schema.org/draft-04/schema#", "$schema": "http://json-schema.org/draft-04/schema#", "comment": "Schema for category validation during creation", "type": "object", "properties": { "name": { "type": "string", "minLength": 1, "maxLength": 128, "pattern": "^[a-zA-Z0-9_-]+$" } }, "required": ["name"], "additionalProperties": false } 

It works, but not as it should, namely: it does not allow whitespace characters in a validated string. such a regular expression with \t or \s included (example: "^[a-zA-Z0-9_-\s]+$" ) fails: phpunit returns JSON syntax is malformed . If anyone in the know - please prompt.

    1 answer 1

    ^[a-zA-Z0-9_-\s]+$ here instead of enumerating the character - and the space character \s , you get an incorrect range _-\s (between the underscore and the space character). You can put \s at the beginning for example: ^[\sa-zA-Z0-9_-]+$ .

    The second point is that the \ sign in the pattern must be escaped. The final option:

     "pattern": "^[\\sa-zA-Z0-9_-]+$" 

    Another trifle: id should be unique for your schema, for example, "id": "http://yourdomain.com/schemas/myschema.json" , you have it coincides with the "$schema" field. But in fact validator is not important.

    PS Regular expressions can be quickly checked online at https://regex101.com/ . You can check your schema for validity at http://www.jsonschemavalidator.net/ by choosing Schema draft v4 on the left and inserting your schema on the right.