conformation
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -27,2 +27,7 @@ 'use strict'; | ||
}, | ||
only: function only(values) { | ||
if (!_lodash2.default.isArray(values)) values = [].slice.apply(arguments); | ||
return this.rule(onlyRule, { values: values }); | ||
}, | ||
validate: function validate(value, context) { | ||
@@ -101,2 +106,10 @@ var errors = []; | ||
function onlyRule(value, params, ctx) { | ||
if (_lodash2.default.isNil(value) || value === '' || _lodash2.default.includes(params.values, value)) { | ||
return { valid: true }; | ||
} else { | ||
return (0, _util.error)('expected one of ' + params.values); | ||
} | ||
} | ||
function mergeArray(objValue, srcValue) { | ||
@@ -103,0 +116,0 @@ if (_lodash2.default.isArray(objValue)) { |
@@ -55,38 +55,42 @@ 'use strict'; | ||
if (!schema) { | ||
errors.push({ | ||
message: 'unexpected key ' + k, | ||
path: k | ||
}); | ||
} else { | ||
var handleResult = function handleResult(result) { | ||
if (result.valid) { | ||
if (result.hasOwnProperty('value')) ctx.converted[k] = result.value;else ctx.converted[k] = value[k]; | ||
} else { | ||
[].push.apply(errors, result.errors.map(function (error) { | ||
if (error.path) { | ||
error.path = k + '.' + error.path; | ||
} else { | ||
error.path = k; | ||
} | ||
var result = schema.validate(value[k], ctx); | ||
return error; | ||
})); | ||
} | ||
}; | ||
function handleResult(result) { | ||
if (result.valid) { | ||
if (result.hasOwnProperty('value')) ctx.converted[k] = result.value;else ctx.converted[k] = value[k]; | ||
} else { | ||
[].push.apply(errors, result.errors.map(function (error) { | ||
if (error.path) { | ||
error.path = k + '.' + error.path; | ||
} else { | ||
error.path = k; | ||
} | ||
var result = schema.validate(value[k], ctx); | ||
if (result.then) { | ||
promises.push(result.then(handleResult)); | ||
} else { | ||
handleResult(result); | ||
return error; | ||
})); | ||
} | ||
} | ||
if (result.then) { | ||
promises.push(result.then(handleResult)); | ||
} else { | ||
handleResult(result); | ||
} | ||
}; | ||
for (var k in value) { | ||
for (var k in params.schema) { | ||
_loop(k); | ||
} | ||
var difference = _lodash2.default.difference(_lodash2.default.keys(value), _lodash2.default.keys(params.schema)); | ||
if (difference.length) { | ||
[].push.apply(errors, difference.map(function (key) { | ||
return { | ||
message: 'unexpected key ' + key, | ||
path: key | ||
}; | ||
})); | ||
} | ||
function finish() { | ||
@@ -93,0 +97,0 @@ _lodash2.default.merge(_ctx, _lodash2.default.omit(ctx, ['parent', 'key', 'converted'])); |
{ | ||
"name": "conformation", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"description": "Yet another object validator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -138,2 +138,9 @@ | ||
}); | ||
it('should have a only() rule', function () { | ||
let schema = Schema.any().only(1, 2); | ||
expect(schema.validate(1).valid).to.be.true; | ||
expect(schema.validate(2).valid).to.be.true; | ||
expect(schema.validate(3).valid).to.be.false; | ||
}); | ||
}); | ||
@@ -140,0 +147,0 @@ |
27215
750