form-schema
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "form-schema", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A form validation tool that traverses the data according to the schema and calls the validators.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -11,3 +11,3 @@ export default class FormField { | ||
validate(value, dataRoot) { | ||
validate(value, ...rest) { | ||
if (typeof value === 'undefined') { | ||
@@ -23,3 +23,3 @@ return this._isRequired ? { | ||
const result = this._validators.reduce(({ isValid, errors }, validator) => { | ||
const { isValid: valid, error } = validator(value, dataRoot); | ||
const { isValid: valid, error } = validator(value, ...rest); | ||
if (!valid) { | ||
@@ -26,0 +26,0 @@ errors.push(error); |
import FormField from './FormField'; | ||
import FormFieldError from './FormFieldError'; | ||
export { FormFieldError as FormFieldError }; | ||
export { FormFieldError }; | ||
@@ -10,3 +10,3 @@ export function field(...args) { | ||
function _validateHelper(data, schema, ancestors = []) { | ||
function _validateHelper(data, schema, ancestors = [], keyPath = []) { | ||
let dataType = typeof data; | ||
@@ -17,3 +17,3 @@ dataType = (dataType === 'object' && Array.isArray(data)) ? 'array' : dataType; | ||
// NOTE: You cannot assume the data at this point is a primitive. | ||
const result = schema.validate(data, ancestors); | ||
const result = schema.validate(data, ancestors, keyPath); | ||
return result; | ||
@@ -23,3 +23,5 @@ } else if (Array.isArray(schema)) { // array | ||
// iterate array and validate element | ||
const resultArr = data.map(x => _validateHelper(x, schema[0], [data, ...ancestors])); | ||
const resultArr = data.map((x, index) => | ||
_validateHelper(x, schema[0], [data, ...ancestors], [index, ...keyPath]) | ||
); | ||
const isValid = resultArr.every(x => x.isValid); | ||
@@ -39,3 +41,3 @@ return { | ||
const resultObj = Object.keys(schema).reduce((acc, key) => { | ||
acc[key] = _validateHelper(data[key], schema[key], [data, ...ancestors]); | ||
acc[key] = _validateHelper(data[key], schema[key], [data, ...ancestors], [key, ...keyPath]); | ||
return acc; | ||
@@ -42,0 +44,0 @@ }, {}); |
@@ -293,3 +293,3 @@ import chai from 'chai'; | ||
describe('validators', () => { | ||
it('should supply field data and its ancestors to the validator', () => { | ||
it('should supply the field data, its ancestors and the keyPath to the validator', () => { | ||
const data = { | ||
@@ -303,5 +303,7 @@ id: 'id1', | ||
}; | ||
const rootFieldsValidator = (fieldData, ancestors) => { | ||
const rootFieldsValidator = (fieldData, ancestors, keyPath) => { | ||
ancestors.should.have.length(1); | ||
ancestors[0].should.equal(data); | ||
keyPath.should.have.length(1); | ||
(keyPath[0] === 'id' || keyPath[0] === 'name').should.be.true; | ||
return { | ||
@@ -311,6 +313,9 @@ isValid: true, | ||
}; | ||
const nestedFieldValidator = (fieldData, ancestors) => { | ||
const nestedFieldValidator = (fieldData, ancestors, keyPath) => { | ||
ancestors.should.have.length(2); | ||
ancestors[0].should.equal(data.nested); | ||
ancestors[1].should.equal(data); | ||
keyPath.should.have.length(2); | ||
keyPath[0].should.equal('random'); | ||
keyPath[1].should.equal('nested'); | ||
return { | ||
@@ -320,6 +325,9 @@ isValid: true, | ||
}; | ||
const arrFieldValidator = (fieldData, ancestors) => { | ||
const arrFieldValidator = (fieldData, ancestors, keyPath) => { | ||
ancestors.should.have.length(2); | ||
ancestors[0].should.equal(data.arr); | ||
ancestors[1].should.equal(data); | ||
keyPath.should.have.length(2); | ||
(keyPath[0] >= 0 && keyPath[0] <= 2).should.be.true; | ||
keyPath[1].should.equal('arr'); | ||
return { | ||
@@ -326,0 +334,0 @@ isValid: true, |
34000
473