Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation errors on non-required fields #49

Closed
cappamontana opened this issue Aug 6, 2014 · 10 comments
Closed

Validation errors on non-required fields #49

cappamontana opened this issue Aug 6, 2014 · 10 comments

Comments

@cappamontana
Copy link

Hi,

First of all, thanks for this great library!
I've encountered some problems validating a schema with optional/non-required properties.
I get this error:

errors: Array[2]
0: ValidationError
code: "FORMAT"
message: "date format validation failed: "
path: "#/date_of_birth"
1: ValidationError
code: "PATTERN"
message: "String does not match pattern: ^[1-9]{1}[0-9]{3} ?[A-Z]{2}$"
params: Object
path: "#/postcode"
valid: false

on this schema:
"website_users":
{
"$schema": "http://json-schema.org/schema#",
"description": "Schema for the website users context",
"type": "object",
"self": {
"vendor": "xxxx",
"name": "website_users",
"format": "jsonschema",
"version": "1-0-0"
},
"properties": {

        "date_of_birth": {
            "type": "string",
            "format": "date" 
        },

        "dealer_postcode": {
            "type": "string",
            "pattern": "^[1-9]{1}[0-9]{3} ?[A-Z]{2}$"
        },
        "email": {
            "type": "string",
            "maxLength": 128
        },
        "email_id": {
            "type": "string",
            "maxLength": 64
        },
        "firstname": {
            "type": "string",
            "maxLength": 64
        },
        "gender": {
            "type": "string", "enum":["man","vrouw"]
        },
        "housenumber": {
            "type": "string",
            "maxLength": 16
        },
        "initials": {
            "type": "string",
            "maxLength": 8
        },
        "kvk_nummer": {
            "type": "string",
            "maxLength": 32
        },
        "language": {
            "type": "string",
            "maxLength": 16
        },
        "lastname": {
            "type": "string",
            "maxLength": 64
        },

        "phonenumber": {
            "type": "string",
            "maxLength": 32
        },
        "postcode": {
            "type": "string",
            "pattern": "^[1-9]{1}[0-9]{3} ?[A-Z]{2}$"
        },
        "street": {
            "type": "string",
            "maxLength": 64
        },
        "tussenvoegsel": {
            "type": "string",
            "maxLength": 16
        },
        "user_id": {
            "type": "string",
            "maxLength": 32
        }
    },
    "required": ["email"],
    "additionalProperties": false
}
  • This is just a fragment of the complete json schema.

I understand that the validation fails on these fields (because they are empty) but they are optional.

Thanks in advance!

@zaggino
Copy link
Owner

zaggino commented Aug 6, 2014

Hi @cappamontana, can you supply the object you're validating with the schema? Ideally make object and schema as small as possible to replicate your issue.

For example, here's a testcase for issue #48 https://github.com/zaggino/z-schema/blob/master/test/issue_48.js

@cappamontana
Copy link
Author

I've just made a simple object so you can see the properties. I guess it is happening because of the empty string values.

var website_users = {
bedrijfsnaam: ""
city: ""
date_of_birth: ""
dealer_id: ""
email: "pon_test@hotmail.com"
firstname: "test"
gender: "man"
housenumber: ""
initials: ""
kvk_nummer: ""
lastname: "pon"
licenseplate: ""
phonenumber: ""
postcode: ""
street: ""
tussenvoegsel: ""
user_id: ""
};

@zaggino
Copy link
Owner

zaggino commented Aug 6, 2014

Ok, the validator is actually doing the right thing - once the properties are present in the object, they must validate. If you wanted to treat empty strings as non-existing properties, we'd need a special option for that, because this behaviour would not respect the json schema specification.

Now there's another question, if we'd treat "" as non-existing, what would we do with null and undefined ? Or 0 or [] or false ?

@cappamontana
Copy link
Author

Yes if they are present they must validate, so I can understand somehow that the DATE field is not validating (because it's a string and not a date) but still these fields are optional and should not cause the complete json to fail. But more important, if we skip the date_of_birth property and look to i.e postcode, the regexp fails (which is right), but this shouldn't mean the rest of the json is not valid, since this is not a required field.
Maybe I'm too much thinking from DB perspective or my understanding of json schema standards is not enough.

@zaggino
Copy link
Owner

zaggino commented Aug 6, 2014

JSON schema is specific that if one thing fails, everything fails. If a property is present, it needs to be validated even if it's an empty string. I could create option dontValidateEmptyStrings which you'd set to true and your case would pass but this behaviour doesn't respect original JSON schema specification.

There is no state like partially valid json, it can be either 100% valid or invalid.

@zaggino
Copy link
Owner

zaggino commented Aug 6, 2014

If you're using this with a DB like MongoDB, I'd suggest to change the types from

"postcode": {
            "type": "string",
            "pattern": "^[1-9]{1}[0-9]{3} ?[A-Z]{2}$"
        }

to:

"postcode": {
            "type": ["string", "null"],
            "pattern": "^[1-9]{1}[0-9]{3} ?[A-Z]{2}$"
        }

and instead of "" put null values into your object. Because in the end, empty strings are evil :-)

@cappamontana
Copy link
Author

Hi Martin,

Thanks for your message. I've fixed the problem by just mapping values to
the json object that are not empty. Also a fail proof version of booleans
and Date's are added so
everything works fine now.

Thanks!

On Wed, Aug 6, 2014 at 4:07 PM, Martin Zagora notifications@github.com
wrote:

If you're using this with a DB like MongoDB, I'd suggest to change the
types from

"postcode": {
"type": "string",
"pattern": "^[1-9]{1}[0-9]{3} ?[A-Z]{2}$"
}

to:

"postcode": {
"type": ["string", "null"],
"pattern": "^[1-9]{1}[0-9]{3} ?[A-Z]{2}$"
}

and instead of "" put null values into your object.


Reply to this email directly or view it on GitHub
#49 (comment).

@cappamontana
Copy link
Author

Hi Martin,

One last thing...I'm getting the error below on

  1. postcode: "0000 aa"

    and regexp ^[0-9]{1}[0-9]{3}(\s)?[A-Za-z]{2}$

It seems like the \ from \s is skipped.

  1. ValidationError
    1. code: "PATTERN"
    2. message: "String does not match pattern:
      ^[0-9]{1}[0-9]{3}(s)?[A-Za-z]{2}$"
    3. params: Object
    4. path: "#/postcode"
      5.
      6.

On Wed, Aug 6, 2014 at 4:28 PM, Tuncay Oner tuncayoner@gmail.com wrote:

Hi Martin,

Thanks for your message. I've fixed the problem by just mapping values to
the json object that are not empty. Also a fail proof version of booleans
and Date's are added so
everything works fine now.

Thanks!

On Wed, Aug 6, 2014 at 4:07 PM, Martin Zagora notifications@github.com
wrote:

If you're using this with a DB like MongoDB, I'd suggest to change the
types from

"postcode": {
"type": "string",
"pattern": "^[1-9]{1}[0-9]{3} ?[A-Z]{2}$"
}

to:

"postcode": {
"type": ["string", "null"],
"pattern": "^[1-9]{1}[0-9]{3} ?[A-Z]{2}$"
}

and instead of "" put null values into your object.


Reply to this email directly or view it on GitHub
#49 (comment).

zaggino added a commit that referenced this issue Aug 6, 2014
@zaggino
Copy link
Owner

zaggino commented Aug 6, 2014

If you have it in a JavaScript code, it needs to be correctly escaped like this: 42f8b74

@zaggino
Copy link
Owner

zaggino commented Aug 6, 2014

If you don't use the double-slash, JSHint should warn you about it, see my screenshot from Brackets:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants