geraintluff / jsv4-php

A (coercive) JSON Schema v4 Validator for PHP
Other
114 stars 31 forks source link

No validation on string regular expression? #22

Open csimplestring opened 9 years ago

csimplestring commented 9 years ago

Hi,

Here is my json schema: "image": { "type": "string", "pattern": "^((http://|https://)?(www)?)+(\.?([\w/]))+\.?([\w])+?$"
},

but when I input data like: { "image": "htt://www.example.com/1.jpg" }

It always validates to be TRUE. So regular expression is not supported yet?

redoneben commented 9 years ago

Regular expression is supported. The problem will occur when you are using a backslash '\' in a regular expression. I had the same problem and the next pattern didn't work because i used a backslash:

"pattern":"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$"

Solving this (quick fix):

"pattern":"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"

I put just an other backslash before a backslash and it worked! So try this:

"pattern": "^((http://|https://)?(www)?)+(\\.?([\w/]))+\\.?([\\w])+?$"

3rgo commented 8 years ago

Sorry for digging that thread up, but I'm having the same issue. I'm using the latest master, and when trying to validate a hex color string using a reference, it fails.

Here is my schema :

{
    "$schema"     : "http://json-schema.org/schema#",
    "id"          : "color.json",
    "definitions" : {
        "color" : {
            "type"    : "string",
            "pattern" : "^#[0-9A-F]{6}$"
        }
    },

    "type" : "object",
    "properties" : {
        "color"   : { "$ref" : "#/definitions/color"   }
    }
}

And here is my JSON :

{
    "color": "#A2"
}

I'm at a loss for what to do here. My schema doesn't seem wrong, because when I run these inputs on JSON Schema Validator, the correct error pops up : String '#A2' does not match regex pattern '^#[0-9A-F]{6}$'.

I also have another regex for dates (YYYY-MM-DD format) that does not work either : ^\\d{4}(\\-\\d{2}){2}$

Thanks for your help