15

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 :

{
    "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, 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

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
suri babu
  • 223
  • 1
  • 2
  • 8

2 Answers2

10

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/

t3chb0t
  • 16,340
  • 13
  • 78
  • 118
  • Tried your change still failing. After the change have you tried validating? – suri babu Dec 30 '14 at 13:45
  • Yes I have. It's probably because you formatted the snippets incorrectly and if you copy them from here their missing a closing parenthesis. – t3chb0t Dec 30 '14 at 13:47
  • Validation for invalid email address is also success. Have you tried giving a invalid email address. – suri babu Dec 30 '14 at 14:36
  • It wasn't part of the question so no ;-] (not yet) – t3chb0t Dec 30 '14 at 14:45
  • OK, can you please try and help me to fix the issue. – suri babu Dec 30 '14 at 14:57
  • I'm not sure if you've noticed but I've edited my answer and added the corrected schema. – t3chb0t Dec 30 '14 at 16:23
  • This schema has other problems; it misuses `id`, and `required` makes no sense at the top. This is 2014 so really the schema should be draft v4, not draft v3. – fge Dec 30 '14 at 21:12
  • Okay, I didn't look at it. It's up to you now what you gonna to with the slightly fixed schema :-) I hope I could help. -- @fge oh and I just thought the OP wrote the last comment.... – t3chb0t Dec 30 '14 at 21:14
  • Thanks t3chb0t, additionalProperties has fixed the issue. Great help. – suri babu Dec 31 '14 at 10:11
6

Changed the schema as per 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
}
suri babu
  • 223
  • 1
  • 2
  • 8