conformation
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -18,4 +18,4 @@ 'use strict'; | ||
_mutate: function _mutate(rule, params) { | ||
return _lodash2.default.mergeWith({}, this, { rules: [{ rule: rule, params: params }] }, mergeArray); | ||
rule: function rule(_rule, params) { | ||
return _lodash2.default.mergeWith({}, this, { rules: [{ rule: _rule, params: params }] }, mergeArray); | ||
}, | ||
@@ -26,3 +26,3 @@ constructor: function constructor() { | ||
required: function required() { | ||
return this._mutate(requiredRule); | ||
return this.rule(requiredRule); | ||
}, | ||
@@ -29,0 +29,0 @@ validate: function validate(value, context) { |
@@ -21,9 +21,9 @@ 'use strict'; | ||
constructor: function constructor(params) { | ||
return this._mutate(constructorRule, params); | ||
return this.rule(constructorRule, params); | ||
}, | ||
items: function items(schema) { | ||
return this._mutate(itemsRule, { schema: schema }); | ||
return this.rule(itemsRule, { schema: schema }); | ||
}, | ||
length: function length(_length) { | ||
return this._mutate(lengthRule, { length: _length }); | ||
return this.rule(lengthRule, { length: _length }); | ||
} | ||
@@ -41,7 +41,15 @@ }); | ||
function itemsRule(value, params, ctx) { | ||
function itemsRule(value, params, _ctx) { | ||
var errors = []; | ||
var promises = []; | ||
var ctx = _lodash2.default.cloneDeep(_ctx); | ||
ctx.parent = value; | ||
ctx.converted = {}; | ||
var path = ctx.path ? ctx.path + '.' : ''; | ||
var _loop = function _loop(i) { | ||
ctx.key = i; | ||
ctx.path = path + i; | ||
var result = params.schema.validate(value[i], ctx); | ||
@@ -51,3 +59,3 @@ | ||
if (result.valid) { | ||
if (result.hasOwnProperty('value')) value[i] = result.value; | ||
if (result.hasOwnProperty('value')) ctx.converted[i] = result.value;else ctx.converted[i] = value[i]; | ||
} else { | ||
@@ -78,6 +86,8 @@ [].push.apply(errors, result.errors.map(function (error) { | ||
function finish() { | ||
_lodash2.default.merge(_ctx, _lodash2.default.omit(ctx, ['parent', 'key', 'converted'])); | ||
if (errors.length) { | ||
return { valid: false, errors: errors }; | ||
} else { | ||
return { valid: true, value: value }; | ||
return { valid: true, value: ctx.converted }; | ||
} | ||
@@ -84,0 +94,0 @@ } |
@@ -21,3 +21,3 @@ 'use strict'; | ||
constructor: function constructor(params) { | ||
return this._mutate(constructorRule, params); | ||
return this.rule(constructorRule, params); | ||
} | ||
@@ -24,0 +24,0 @@ }); |
@@ -25,3 +25,3 @@ 'use strict'; | ||
constructor: function constructor(params) { | ||
return this._mutate(constructorRule, params); | ||
return this.rule(constructorRule, params); | ||
} | ||
@@ -28,0 +28,0 @@ }); |
@@ -25,9 +25,9 @@ 'use strict'; | ||
constructor: function constructor(params) { | ||
return this._mutate(constructorRule, params); | ||
return this.rule(constructorRule, params); | ||
}, | ||
date: function date() { | ||
return this._mutate(dateRule); | ||
return this.rule(dateRule); | ||
}, | ||
format: function format(_format) { | ||
return this._mutate(formatRule, { format: _format }); | ||
return this.rule(formatRule, { format: _format }); | ||
} | ||
@@ -34,0 +34,0 @@ }); |
@@ -21,6 +21,6 @@ 'use strict'; | ||
constructor: function constructor(params) { | ||
return this._mutate(constructorRule, params); | ||
return this.rule(constructorRule, params); | ||
}, | ||
integer: function integer() { | ||
return this._mutate(integerRule); | ||
return this.rule(integerRule); | ||
} | ||
@@ -27,0 +27,0 @@ }); |
@@ -25,6 +25,6 @@ 'use strict'; | ||
constructor: function constructor(params) { | ||
return this._mutate(constructorRule, params); | ||
return this.rule(constructorRule, params); | ||
}, | ||
keys: function keys(schema) { | ||
return this._mutate(keysRule, { schema: schema }); | ||
return this.rule(keysRule, { schema: schema }); | ||
} | ||
@@ -42,8 +42,15 @@ }); | ||
function keysRule(value, params, ctx) { | ||
function keysRule(value, params, _ctx) { | ||
var errors = []; | ||
var promises = []; | ||
var ctx = _lodash2.default.cloneDeep(_ctx); | ||
ctx.parent = value; | ||
ctx.converted = {}; | ||
var path = ctx.path ? ctx.path + '.' : ''; | ||
var _loop = function _loop(k) { | ||
var schema = params.schema[k]; | ||
ctx.key = k; | ||
ctx.path = path + k; | ||
@@ -58,3 +65,3 @@ if (!schema) { | ||
if (result.valid) { | ||
if (result.hasOwnProperty('value')) value[k] = result.value; | ||
if (result.hasOwnProperty('value')) ctx.converted[k] = result.value;else ctx.converted[k] = value[k]; | ||
} else { | ||
@@ -88,6 +95,8 @@ [].push.apply(errors, result.errors.map(function (error) { | ||
function finish() { | ||
_lodash2.default.merge(_ctx, _lodash2.default.omit(ctx, ['parent', 'key', 'converted'])); | ||
if (errors.length) { | ||
return { valid: false, errors: errors }; | ||
} else { | ||
return { valid: true, value: value }; | ||
return { valid: true, value: ctx.converted }; | ||
} | ||
@@ -94,0 +103,0 @@ } |
@@ -21,6 +21,6 @@ 'use strict'; | ||
constructor: function constructor(params) { | ||
return this._mutate(constructorRule, params); | ||
return this.rule(constructorRule, params); | ||
}, | ||
regex: function regex(pattern) { | ||
return this._mutate(regexRule, { pattern: pattern }); | ||
return this.rule(regexRule, { pattern: pattern }); | ||
} | ||
@@ -27,0 +27,0 @@ }); |
{ | ||
"name": "conformation", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Yet another object validator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -27,4 +27,4 @@ | ||
let schema = Schema.any() | ||
._mutate(rule1, {a: 1}) | ||
._mutate(rule2, {b: 2}); | ||
.rule(rule1, {a: 1}) | ||
.rule(rule2, {b: 2}); | ||
@@ -47,4 +47,4 @@ schema.validate('the value'); | ||
let schema = Schema.any() | ||
._mutate(rule1) | ||
._mutate(rule2); | ||
.rule(rule1) | ||
.rule(rule2); | ||
@@ -70,4 +70,4 @@ expect(schema.validate()).to.eql({ | ||
let schema = Schema.any() | ||
._mutate(rule1) | ||
._mutate(rule2); | ||
.rule(rule1) | ||
.rule(rule2); | ||
@@ -92,4 +92,4 @@ expect(schema.validate()).to.eql({ | ||
let schema = Schema.any() | ||
._mutate(rule1) | ||
._mutate(rule2); | ||
.rule(rule1) | ||
.rule(rule2); | ||
@@ -112,4 +112,4 @@ expect(schema.validate()).to.eql({ | ||
let schema = Schema.any() | ||
._mutate(rule1) | ||
._mutate(rule2); | ||
.rule(rule1) | ||
.rule(rule2); | ||
@@ -271,2 +271,30 @@ return schema.validate().then(function (result) { | ||
}); | ||
it('should set path and key properly', function () { | ||
let schema = Schema.object().keys({ | ||
a: Schema.any().rule(function (value, params, ctx) { | ||
expect(ctx.key).to.equal('a'); | ||
return {valid: true}; | ||
}), | ||
b: Schema.object().keys({ | ||
c: Schema.any().rule(function (value, params, ctx) { | ||
expect(ctx.key).to.equal('c'); | ||
expect(ctx.path).to.equal('b.c'); | ||
ctx.meta = 5; | ||
return {valid: true}; | ||
}) | ||
}), | ||
d: Schema.any().rule(function (value, params, ctx) { | ||
expect(ctx.key).to.equal('d'); | ||
expect(ctx.path).to.equal('d'); | ||
return {valid: true}; | ||
}) | ||
}); | ||
let ctx = {}; | ||
schema.validate({a: 1, b: {c: 2}, d: 3}, ctx); | ||
expect(ctx.meta).to.equal(5); | ||
}) | ||
}); | ||
@@ -273,0 +301,0 @@ |
26416
731