Checking Json Schema Dynamic Key

Facing problems with checking circuitry.

circuit:

{ "type": "object", "$schema": "http://json-schema.org/draft-03/schema", "id": "#", "required": true, "patternProperties": { "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[AZ]{2,6}$": { "type": "object", "required": true, "properties": { "_from": { "id": "_from", "type": "string", "required": true }, "message": { "type": "object", "id": "message", "properties": { "detail": { "type": "string", "id": "detail", "required": true }, "from": { "type": "string", "id": "from", "required": true } } } } } } } 

json:

 { "tom@example.com": { "_from": "giles@gmail.com", "message": { "from": "Giles@gmail.com", "detail": "AnyonewanttomeetmeinParis" } }, "harry@example.com": { "_from": "giles@gmail.com", "message": { "from": "Giles@gmail.com", "detail": "AnyonewanttomeetmeinParis" } } } 

Here, the key email address is dynamic, as it does not validate the regular expression to validate email.

Could you advise me to fix the circuit.

I test usage: http://json-schema-validator.herokuapp.com/index.jsp

+8
jsonschema json-schema-validator
source share
2 answers

I see in your template that you seem to have forgotten to escape from some characters or not done it right:

 "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[AZ]{2,6}$" 

and this causes an error that you can see when you hover over the link at the top of the validator:

enter image description here

it should be:

 "^[A-Z0-9\\._%\\+-]+@[A-Z0-9\\.-]+\\.[AZ]{2,6}$" 

or without escaping inner / character classes, but I would use the first pattern because I think its intent is clearer:

 "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[AZ]{2,6}$" 

You need to have two \ , because the first \ is escape for the second \ . It will not work with one, because there is no escape sequence , for example, \. or \+ in JavaScript. You want to have \ in the template itself.

However, json schema patternProperties is case sensitive by default, so you need to extend the email template by adding az to it:

 "^[A-Za-z0-9\\._%\\+-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,6}$" 

(I did not find another way to make it case sensitive)

You also need to exclude any other property names by adding "additionalProperties": false next to patternProperties or else it will catch everything else that does not match the pattern.

The working diagram should look like this:

 { "type": "object", "$schema": "http://json-schema.org/draft-03/schema", "id": "#", "required": true, "patternProperties": { "^[A-Za-z0-9\\._%\\+-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,6}$": { "type": "object", "required": true, "properties": { "_from": { "id": "_from", "type": "string", "required": true }, "message": { "type": "object", "id": "message", "properties": { "detail": { "type": "string", "id": "detail", "required": true }, "from": { "type": "string", "id": "from", "required": true } } } } } }, "additionalProperties": false } 

I tested it: http://jsonschemalint.com/

+7
source share

Scheme changed according to draft 04:

 { "type": "object", "$schema": "http://json-schema.org/draft-04/schema", "patternProperties": { "^[A-Za-z0-9\\._%\\+-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,6}$": { "type": "object", "properties": { "__from": { "type": "string" }, "message": { "type": "object", "properties": { "from": { "type": "string" }, "detail": { "type": "string" } }, "required": [ "from","detail"] } }, "required": [ "__from","message"] } }, "additionalProperties": false } 
+4
source share

All Articles