Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the type of validation failed to the error. also added a validateAll method that returns all the failed validation rules of a field. fixes issue #60 #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

export default class ValidationError extends Error {
constructor(message, path) {
constructor(message, path, type) {
super(message);

Object.defineProperty(this, 'path', {
Expand All @@ -14,6 +14,12 @@ export default class ValidationError extends Error {
writable: true,
value: path
});
Object.defineProperty(this, 'type', {
enumerable: false,
configurable: true,
writable: true,
value: type
});

if (Error.captureStackTrace) {
Error.captureStackTrace(this, ValidationError);
Expand Down
49 changes: 47 additions & 2 deletions src/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,51 @@ export default class Property {
return null;
}

/**
* Validate given `value`
*
* @example
* prop.type(Number)
* assert(prop.validate(2) == null)
* assert(prop.validate('hello world') instanceof Error)
*
* @param {Mixed} value - value to validate
* @param {Object} ctx - the object containing the value
* @param {String} [path] - path of the value being validated
* @return {ValidationError}
*/

validateAll(value, ctx, path = this.name) {
const types = Object.keys(this.registry);
const done = {};
const errors = []
let err;

// Required first
err = this._run('required', value, ctx, path);
if (err) errors.push(err);

// No need to continue if value is null-ish
if (value == null) return null;

// Run type second
err = this._run('type', value, ctx, path);
if (err) errors.push(err);

// Make sure required and run are not executed again
done.required = true;
done.type = true;

// Run the rest
for (let type of types) {
if (done[type]) continue;
err = this._run(type, value, ctx, path);
if (err) errors.push(err);
}

return errors.length ? errors : null;
}

/**
* Run validator of given `type`
*
Expand All @@ -327,7 +372,7 @@ export default class Property {
_run(type, value, ctx, path) {
if (!this.registry[type]) return;
const schema = this._schema;
const {args, fn} = this.registry[type];
const { args, fn } = this.registry[type];
let validator = fn || schema.validators[type];
let valid = validator(value, ctx, ...args, path);
if (!valid) return this._error(type, ctx, args, path);
Expand Down Expand Up @@ -371,6 +416,6 @@ export default class Property {
message = message(path, ctx, ...args);
}

return new ValidationError(message, path);
return new ValidationError(message, path, type);
}
}
38 changes: 38 additions & 0 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,44 @@ export default class Schema {
return errors;
}

/**
* Validate given `obj`.
*
* @example
* const schema = new Schema({ name: { required: true }})
* const errors = schema.validate({})
* assert(errors.length == 1)
* assert(errors[0].message == 'name is required')
* assert(errors[0].path == 'name')
*
* @param {Object} obj - the object to validate
* @param {Object} [opts] - options, see [Schema](#schema-1)
* @return {Array}
*/

validateAll(obj, opts = {}) {
opts = Object.assign(this.opts, opts);

let errors = [];

if (opts.typecast) {
this.typecast(obj);
}

if (opts.strip !== false) {
this.strip(obj);
}

for (let [path, prop] of Object.entries(this.props)) {
walk(path, obj, (key, value) => {
const err = prop.validateAll(value, obj, key);
if (err) errors = errors.concat(err);
});
}

return errors;
}

/**
* Assert that given `obj` is valid.
*
Expand Down