Multiple patterns?

910 views
Skip to first unread message

Michaël Rouges

unread,
Mar 14, 2016, 11:53:48 PM3/14/16
to JSON Schema
Hi all,

Is there a way to specify a set of validation patterns, please, for an unique field?

A classical usage: to test the strongness of a password.

Simon Sprott

unread,
Mar 15, 2016, 4:35:52 AM3/15/16
to JSON Schema

Hi

A couple of suggestions come to mind, depending on if you want all the rules to validate (AND) or any of the rules to validate (OR).

You could do it all in a single pattern (see Regex ANDs for ideas for that - one of the post suggest re-structuring an AND using NOT and OR)

or 

You could keep adding to the rules for Password using an allOf or anyOf construct (depending on AND or OR requirements).
But this is a bit messy, unintuitive, and in the long term probably error prone.






{
    "type": "object",
    "title": "My schema",
    "additionalProperties": false,
    "properties": {
        "Password": {
            "type": "string",
            "description": "Min 5 chars",
            "pattern": "^[a-z\\d]{5,}$"
        }
    },
    "allOf": [
        {
            "type": "object",
            "properties": {
                "Password": {
                    "type": "string",
                    "description": "1 char and 1 number",
                    "pattern": "^([a-z]+\\d+|\\d+[a-z]+)\\w*$"
                }
            }
        }
    ]
}


Overall I would suggest that this is probably a business rule that belongs outside of the schema?

Regards 
     Simon

Michaël Rouges

unread,
Mar 15, 2016, 2:33:33 PM3/15/16
to JSON Schema
Hi,

Thanks for your reply.

I see... the merged solutions seems fine, on long-term, but not parsed by a validator.

I hope the future versions supports a thing like patterns as an array.


Overall I would suggest that this is probably a business rule that belongs outside of the schema?

Maybe, but my goal is to reduce errors, on data submission, to its max.

Michaël Rouges

unread,
Mar 16, 2016, 5:04:58 AM3/16/16
to JSON Schema
Hi,

Just for info, you can declare your "allOf" on the field declaration, avoiding the long-term problem. ;)


Le mardi 15 mars 2016 09:35:52 UTC+1, Simon Sprott a écrit :
Message has been deleted

Simon Sprott

unread,
Mar 16, 2016, 6:20:28 AM3/16/16
to JSON Schema
Yep, that cleans it up.


{
    "type": "object",
    "title": "My schema",
    "additionalProperties": false,
    "properties": {
        "Password": {
            "type": "string",
            "description": "Min 5 chars",
            "pattern": "^[a-z\\d]{5,}$",
            "allOf": [
                {
Reply all
Reply to author
Forward
0 new messages