Comparing version 1.2.0 to 1.3.0
@@ -82,2 +82,3 @@ 'use strict'; | ||
* @param {*} data The data (value) to validate | ||
* @param {Object} [opts={partial: false}] Specific options for validation process | ||
* @param {string|null} [key=null] Key for tracking parent in nested iterations | ||
@@ -92,7 +93,9 @@ * @param {Array<{type: string, sub: string, key: string, value: *, message: string}>} [errors=[]] An error array | ||
validate: function validate(def, data) { | ||
var key = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; | ||
var errors = arguments.length <= 3 || arguments[3] === undefined ? [] : arguments[3]; | ||
var rejectOnFail = arguments.length <= 4 || arguments[4] === undefined ? true : arguments[4]; | ||
var opts = arguments.length <= 2 || arguments[2] === undefined ? { partial: false } : arguments[2]; | ||
var key = arguments.length <= 3 || arguments[3] === undefined ? null : arguments[3]; | ||
var errors = arguments.length <= 4 || arguments[4] === undefined ? [] : arguments[4]; | ||
var rejectOnFail = arguments.length <= 5 || arguments[5] === undefined ? true : arguments[5]; | ||
var curData = data; | ||
def.opts = opts; | ||
var props = rules.getProps(def, data); | ||
@@ -99,0 +102,0 @@ if (!def.type) throw new Error('Model properties must define a \'type\''); |
@@ -70,6 +70,10 @@ 'use strict'; | ||
// Don't run if undefined on required | ||
if (def.required && value === undefined) { | ||
if (def.required && value === undefined && !def.opts.partial) { | ||
errors.push({ type: 'required', sub: 'default', key: key, value: value, message: 'Property \'' + key + '\' is required' }); | ||
return value; | ||
} | ||
// Don't run if undefined and partial | ||
if (value === undefined && def.opts.partial) { | ||
return value; | ||
} | ||
// Execute check | ||
@@ -76,0 +80,0 @@ return types.check({ def: parsedDef, key: key, value: value, fail: fail, errors: errors }); |
@@ -34,3 +34,3 @@ 'use strict'; | ||
var promises = context.value.map(function (elem, idx) { | ||
return _rules2.default.validate(context.def.values, elem, context.key + '[' + idx + ']', context.errors, false); | ||
return _rules2.default.validate(context.def.values, elem, context.def.opts, context.key + '[' + idx + ']', context.errors, false); | ||
}); | ||
@@ -37,0 +37,0 @@ return _bluebird2.default.all(promises); |
@@ -32,3 +32,3 @@ 'use strict'; | ||
_lodash2.default.forOwn(context.def.keys, function (keyDef, key) { | ||
promises[key] = _rules2.default.validate(keyDef, context.value[key], '' + keyPrefix + key, context.errors, false).then(function (val) { | ||
promises[key] = _rules2.default.validate(keyDef, context.value[key], context.def.opts, '' + keyPrefix + key, context.errors, false).then(function (val) { | ||
if (!context.value.hasOwnProperty(key) && val === undefined) missingKeys.push(key); | ||
@@ -66,3 +66,3 @@ return val; | ||
_lodash2.default.forOwn(context.value, function (val, key) { | ||
promises[key] = _rules2.default.validate(context.def.values, val, '' + keyPrefix + key, context.errors, false); | ||
promises[key] = _rules2.default.validate(context.def.values, val, context.def.opts, '' + keyPrefix + key, context.errors, false); | ||
}); | ||
@@ -69,0 +69,0 @@ return _bluebird2.default.props(promises); |
{ | ||
"name": "obey", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Data modelling and validation library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,2 +19,3 @@ # Obey | ||
- [Validation](#validation) | ||
- [Validating Partials](#validating-partials) | ||
- [Validation Error Handling](#validation-error-handling) | ||
@@ -112,2 +113,15 @@ - [Definition Properties](#definition-properties) | ||
### Validating Partials | ||
The `validate` method has the ability to validate partial data objects: | ||
```javascript | ||
// Allow partial validation by supplying second argument with `partial: true` | ||
userModel.validate({ /* some (partial) data */ }, { partial: true }) | ||
``` | ||
The default for the partial option is `false`, but passing `true` will allow for validation of an object containing a subset (i.e. will not throw errors for `required` properties). | ||
The common use-case for validating partials is `PATCH` updates. | ||
### Validation Error Handling | ||
@@ -114,0 +128,0 @@ |
44921
802
328