Comments (10)
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
from z-schema.
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: "[email protected]"
firstname: "test"
gender: "man"
housenumber: ""
initials: ""
kvk_nummer: ""
lastname: "pon"
licenseplate: ""
phonenumber: ""
postcode: ""
street: ""
tussenvoegsel: ""
user_id: ""
};
from z-schema.
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
?
from z-schema.
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.
from z-schema.
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.
from z-schema.
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 :-)
from z-schema.
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 [email protected]
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).
from z-schema.
Hi Martin,
One last thing...I'm getting the error below on
-
postcode: "0000 aa"
and regexp ^[0-9]{1}[0-9]{3}(\s)?[A-Za-z]{2}$
It seems like the \ from \s is skipped.
- ValidationError
- code: "PATTERN"
- message: "String does not match pattern:
^[0-9]{1}[0-9]{3}(s)?[A-Za-z]{2}$" - params: Object
- path: "#/postcode"
5.
6.
On Wed, Aug 6, 2014 at 4:28 PM, Tuncay Oner [email protected] 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 [email protected]
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).
from z-schema.
If you have it in a JavaScript code, it needs to be correctly escaped like this: 42f8b74
from z-schema.
If you don't use the double-slash, JSHint should warn you about it, see my screenshot from Brackets:
from z-schema.
Related Issues (20)
- add keyword in error objects of getLastErrors()
- Allow to specify errors to be reported
- why when schema type is string, there is no error when the value type is integer or number ? HOT 2
- Update to core-js 3.x
- Doesn't validate minimum and maximum on numbers HOT 1
- Versions mismatch cause npm error HOT 4
- Remove core-js HOT 2
- Missing type for Validator.validate(json, schema, options) HOT 3
- 'email' format accepts email addresses with several types of whitespace. HOT 3
- 'ARRAY_ADDITIONAL_ITEMS' error unreachable?
- Is there any approach to suppress an error code HOT 2
- Local schemas not being retrieved from cache
- zSchema doesn't support json-schema post 2020-12
- Vulnerabilities introduced by package validator HOT 2
- Vulnerability in Validator JS HOT 1
- Update Validator to 13.7.0 HOT 1
- `lastReport` absent in `index.d.ts` HOT 2
- Update commander as it's version marked as EOL
- Support for Unicode Properties
- List on json-schema.org HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from z-schema.