Json schema dynamic key validation

suri babu picture suri babu · Dec 30, 2014 · Viewed 10.4k times · Source

Facing an issue with schema validation.

schema :

{
    "type": "object",
    "$schema": "http://json-schema.org/draft-03/schema",
    "id": "#",
    "required": true,
    "patternProperties": {
        "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-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
                        }
                    }
                }
            }
        }
    }
}

json :

{
    "[email protected]": {
        "_from": "[email protected]",
        "message": {
            "from": "[email protected]",
            "detail": "AnyonewanttomeetmeinParis"
        }
    },
    "[email protected]": {
        "_from": "[email protected]",
        "message": {
            "from": "[email protected]",
            "detail": "AnyonewanttomeetmeinParis"
        }
    }
}

Here the key email address is dynamic, somehow it doesn't validate regex for email validation.

Can you please advise me to correct the schema.

I am validating using : http://json-schema-validator.herokuapp.com/index.jsp

Answer

t3chb0t picture t3chb0t · Dec 30, 2014

I see in your pattern that you seem to have forgotten to escape some characters or didn't do it correctly:

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

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

enter image description here

it should be:

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

or without escaping the inner/class characters but I'd use the first pattern because I think its intention is clearer:

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

You need to have two \ because the first \ is an escape for the second \. With a single one it wouldn't work because there is no escape sequence like \. or \+ in javascript. You want to have a \in the pattern itself.

However json schema patternProperties are case sensitive by default so you need to extend your email pattern by adding a-z to it:

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

(I didn't find any other way to make it case insensitive)

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

The working schema should then 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've tested it on: http://jsonschemalint.com/