diff --git a/README.md b/README.md index 3c37ab7..70be95f 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/jjv.js b/lib/jjv.js index e3047fe..ef6e53d 100644 --- a/lib/jjv.js +++ b/lib/jjv.js @@ -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) {