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

Allow empty value #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ if (!errors) {
}
```

For ease of use, email and uri formats accept a allowEmptyValue configuration

```javascript
env.addSchema('foo', {
type: 'object',
properties: {
email: {
type: 'string',
format: 'email',
allowEmptyValue: true
},
uri: {
type: 'string',
format: 'uri',
allowEmptyValue: true
}
}
});
```

It is also possible to validate objects against unregistered and/or
unnamed schemas by supplying the schema object directly. For example:

Expand Down
8 changes: 6 additions & 2 deletions lib/jjv.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@
'hostname': function (v) {
return v.length < 256 && (/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$/).test(v);
},
'uri': function (v) {
'uri': function (v, schema) {
if(v == '' && schema.allowEmptyValue)
return true;
return (/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/).test(v);
},
'email': function (v) { // email, ipv4 and ipv6 adapted from node-validator
'email': function (v, schema) { // email, ipv4 and ipv6 adapted from node-validator
if(v == '' && schema.allowEmptyValue)
return true;
return (/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/).test(v);
},
'ipv4': function (v) {
Expand Down