Skip to content

Commit

Permalink
rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 14, 2016
1 parent 6fd19be commit 5252948
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 119 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ json separate from validating it, via the `cast` method.
- [`yup.reach(schema: Schema, path: string, value: ?object, context: ?object): Schema`](#yupreachschema-schema-path-string-value-object-context-object-schema)
- [`yup.addMethod(schemaType: Schema, name: string, method: ()=> Schema): void`](#yupaddmethodschematype-schema-name-string-method--schema-void)
- [`yup.ref(path: string, options: { contextPrefix: string }): Ref`](#yuprefpath-string-options--contextprefix-string--ref)
- [`yup.lazy((value: any) => Schema): Lazy`](#yuplazyvalue-any--schema-lazy)
- [`ValidationError(errors: string | Array<string>, value: any, path: string)`](#validationerrorerrors-string--arraystring-value-any-path-string)
- [mixed](#mixed)
- [`mixed.clone(): Schema`](#mixedclone-schema)
Expand Down
35 changes: 19 additions & 16 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ inherits(ArraySchema, MixedSchema, {
_cast: function _cast(_value, _opts) {
var _this2 = this;

var value = MixedSchema.prototype._cast.call(this, _value);
var value = MixedSchema.prototype._cast.call(this, _value, _opts);

//should ignore nulls here
if (!this._typeCheck(value) || !this._subType) return value;
Expand All @@ -70,38 +70,41 @@ inherits(ArraySchema, MixedSchema, {
return _this2._subType.cast(v, _opts);
});
},
_validate: function _validate(_value, _opts, _state) {
_validate: function _validate(_value) {
var _this3 = this;

var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

var errors = [],
context,
subType,
schema,
endEarly,
recursive;

_state = _state || {};
context = _state.parent || (_opts || {}).context;
schema = this._resolve(context);
subType = schema._subType;
endEarly = schema._option('abortEarly', _opts);
recursive = schema._option('recursive', _opts);
subType = this._subType;
endEarly = this._option('abortEarly', options);
recursive = this._option('recursive', options);

return MixedSchema.prototype._validate.call(this, _value, _opts, _state).catch(endEarly ? null : function (err) {
return MixedSchema.prototype._validate.call(this, _value, options).catch(endEarly ? null : function (err) {
errors = err;
return err.value;
}).then(function (value) {
if (!recursive || !subType || !schema._typeCheck(value)) {
if (!recursive || !subType || !_this3._typeCheck(value)) {
if (errors.length) throw errors[0];
return value;
}

var result = value.map(function (item, key) {
var path = (_state.path || '') + '[' + key + ']',
state = _extends({}, _state, { path: path, key: key, parent: value });
var path = (options.path || '') + '[' + key + ']';

// object._validate note for isStrict explanation
var innerOptions = _extends({}, options, { path: path, key: key, strict: true, parent: value });

if (subType.validate) return subType.validate(item, innerOptions);

return subType._validate(item, _opts, state);
return true;
});

result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, _state.path, errors);
result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, options.path, errors);

return result.then(function () {
return value;
Expand Down
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

var mixed = require('./mixed'),
bool = require('./boolean'),
Ref = require('./util/reference');
Ref = require('./util/reference'),
Lazy = require('./util/lazy');

var isSchema = function isSchema(schema) {
return schema && !!schema.__isYupSchema__;
Expand All @@ -24,6 +25,9 @@ module.exports = {
ref: function ref(key, options) {
return new Ref(key, options);
},
lazy: function lazy(fn) {
return new Lazy(fn);
},

isSchema: isSchema,

Expand Down
66 changes: 37 additions & 29 deletions lib/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,20 @@ SchemaType.prototype = {
if (this._nullable && v === null) return true;
return !this._typeCheck || this._typeCheck(v);
},
resolve: function resolve(context, parent) {
if (this._conditions.length) {
return this._conditions.reduce(function (schema, match) {
return match.resolve(schema, match.getValue(parent, context));
}, this);
}

return this;
},
cast: function cast(value) {
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

var schema = this._resolve(opts.context, opts.parent);
var schema = this.resolve(opts.context, opts.parent);

return schema._cast(value, opts);
},
_cast: function _cast(_value) {
Expand All @@ -116,65 +126,63 @@ SchemaType.prototype = {
return transform.call(_this2, value, _value);
}, _value);

if (value === undefined && _.has(this, '_default')) value = this.default();
if (value === undefined && _.has(this, '_default')) {
value = this.default();
}

return value;
},
_resolve: function _resolve(context, parent) {
if (this._conditions.length) {
return this._conditions.reduce(function (schema, match) {
return match.resolve(schema, match.getValue(parent, context));
}, this);
}
validate: function validate(value) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var cb = arguments[2];

return this;
if (typeof options === 'function') cb = options, options = {};

var schema = this.resolve(options.context, options.parent);

return nodeify(schema._validate(value, options), cb);
},


//-- tests
_validate: function _validate(_value) {
var _this3 = this;

var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var state = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

var context = options.context,
parent = state.parent,
value = _value,
var value = _value,
schema = void 0,
endEarly = void 0,
isStrict = void 0;

schema = this._resolve(context, parent);
isStrict = schema._option('strict', options);
endEarly = schema._option('abortEarly', options);
schema = this;
isStrict = this._option('strict', options);
endEarly = this._option('abortEarly', options);

var path = state.path;
var path = options.path;
var label = this._label;

if (!state.isCast && !isStrict) value = schema._cast(value, options);

if (!isStrict) {
value = this._cast(value, options, options);
}
// value is cast, we can check if it meets type requirements
var validationParams = { value: value, path: path, state: state, schema: schema, options: options, label: label };
var validationParams = { value: value, path: path, schema: this, options: options, label: label };
var initialTests = [];

if (schema._typeError) initialTests.push(schema._typeError(validationParams));
if (schema._typeError) initialTests.push(this._typeError(validationParams));

if (schema._whitelistError) initialTests.push(schema._whitelistError(validationParams));
if (this._whitelistError) initialTests.push(this._whitelistError(validationParams));

if (schema._blacklistError) initialTests.push(schema._blacklistError(validationParams));
if (this._blacklistError) initialTests.push(this._blacklistError(validationParams));

return runValidations(initialTests, endEarly, value, path).then(function () {
return runValidations(schema.tests.map(function (fn) {
return runValidations(_this3.tests.map(function (fn) {
return fn(validationParams);
}), endEarly, value, path);
}).then(function () {
return value;
});
},
validate: function validate(value, options, cb) {
if (typeof options === 'function') cb = options, options = {};

return nodeify(this._validate(value, options, {}), cb);
},
isValid: function isValid(value, options, cb) {
if (typeof options === 'function') cb = options, options = {};

Expand Down
120 changes: 57 additions & 63 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,19 @@ var _require = require('./util/_');

var isObject = _require.isObject;
var transform = _require.transform;
var assign = _require.assign;
var inherits = _require.inherits;
var collectErrors = _require.collectErrors;
var isSchema = _require.isSchema;
var has = _require.has;


var isRecursive = function isRecursive(schema) {
return (schema._subType || schema) === '$this';
};

c.type('altCamel', function (str) {
var result = c.camel(str),
idx = str.search(/[^_]/);

return idx === 0 ? result : str.substr(0, idx) + result;
});

var childSchema = function childSchema(field, parent) {
if (!isRecursive(field)) return field;

return field.of ? field.of(parent) : parent;
};

var scopeError = function scopeError(value) {
return function (err) {
err.value = value;
Expand Down Expand Up @@ -89,64 +78,58 @@ inherits(ObjectSchema, MixedSchema, {
return isObject(value) || typeof value === 'function';
},
_cast: function _cast(_value) {
var _opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var _this3 = this;

var schema = this,
value = MixedSchema.prototype._cast.call(schema, _value);
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

var value = MixedSchema.prototype._cast.call(this, _value, opts);

//should ignore nulls here
if (!schema._typeCheck(value)) return value;
if (value === undefined) return this.default();

if (!this._typeCheck(value)) return value;

var fields = schema.fields,
strip = schema._option('stripUnknown', _opts) === true,
var fields = this.fields,
strip = this._option('stripUnknown', opts) === true,
extra = Object.keys(value).filter(function (v) {
return schema._nodes.indexOf(v) === -1;
return _this3._nodes.indexOf(v) === -1;
}),
props = schema._nodes.concat(extra);
props = this._nodes.concat(extra);

schema.withMutation(function () {
var innerOptions = _extends({}, _opts, { parent: {} });
var innerOptions = _extends({}, opts, { parent: {} });

value = transform(props, function (obj, prop) {
var field = fields[prop];
var exists = has(value, prop);
value = transform(props, function (obj, prop) {
var field = fields[prop];
var exists = has(value, prop);

if (Ref.isRef(field)) {
var refValue = field.getValue(obj, innerOptions.context);
if (field) {
var fieldValue = void 0;

if (refValue !== undefined) obj[prop] = refValue;
} else if (exists && field) {
tempClearDefault(schema, function () {
var fieldSchema = childSchema(field, schema.default(undefined));
if (field._strip === true) return;

if (fieldSchema._strip !== true) {
obj[prop] = fieldSchema.cast(value[prop], innerOptions);
}
});
} else if (exists && !strip) obj[prop] = value[prop];else if (field) {
var fieldDefault = field.default ? field.default() : undefined;
fieldValue = field.cast(value[prop], innerOptions);

if (fieldDefault !== undefined) obj[prop] = fieldDefault;
}
}, innerOptions.parent);
});
if (fieldValue !== undefined) obj[prop] = fieldValue;
} else if (exists && !strip) obj[prop] = value[prop];
}, innerOptions.parent);

return value;
},
_validate: function _validate(_value, _opts, _state) {
_validate: function _validate(_value) {
var _this4 = this;

var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

var errors = [],
state = _state || {},
context,
schema,
endEarly,
isStrict,
recursive;

context = state.parent || (_opts || {}).context;
schema = this._resolve(context);
endEarly = schema._option('abortEarly', _opts);
recursive = schema._option('recursive', _opts);
isStrict = this._option('strict', opts);
endEarly = this._option('abortEarly', opts);
recursive = this._option('recursive', opts);

return MixedSchema.prototype._validate.call(this, _value, _opts, state).catch(endEarly ? null : function (err) {
return MixedSchema.prototype._validate.call(this, _value, opts).catch(endEarly ? null : function (err) {
errors.push(err);
return err.value;
}).then(function (value) {
Expand All @@ -156,14 +139,24 @@ inherits(ObjectSchema, MixedSchema, {
return value;
}

var result = schema._nodes.map(function (key) {
var path = (state.path ? state.path + '.' : '') + key,
field = childSchema(schema.fields[key], schema);
var result = _this4._nodes.map(function (key) {
var path = (opts.path ? opts.path + '.' : '') + key,
field = _this4.fields[key],
innerOptions = _extends({}, opts, { key: key, path: path, parent: value });

if (field) {
// inner fields are always strict:
// 1. this isn't strict so we just cast the value leaving nested values already cast
// 2. this is strict in which case the nested values weren't cast either
innerOptions.strict = true;

if (field.validate) return field.validate(value[key], innerOptions);
}

return field._validate(value[key], _opts, _extends({}, state, { key: key, path: path, parent: value }));
return true;
});

result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, state.path, errors);
result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, opts.path, errors);

return result.then(function () {
return value;
Expand All @@ -181,7 +174,7 @@ inherits(ObjectSchema, MixedSchema, {
var excludes = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];

var next = this.clone(),
fields = assign(next.fields, schema);
fields = _extends(next.fields, schema);

if (!Array.isArray(excludes[0])) excludes = [excludes];

Expand Down Expand Up @@ -257,14 +250,15 @@ function unknown(ctx, value) {

// ugly optimization avoiding a clone. clears default for recursive
// cast and resets it below;
function tempClearDefault(schema, fn) {
var hasDflt = has(schema, '_default'),
dflt = schema._default;

fn(schema);

if (hasDflt) schema.default(dflt);else delete schema._default;
}
// function tempClearDefault(schema, fn) {
// let hasDflt = has(schema, '_default')
// , dflt = schema._default;
//
// fn(schema)
//
// if (hasDflt) schema.default(dflt)
// else delete schema._default
// }

function sortFields(fields) {
var excludes = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
Expand Down
Loading

0 comments on commit 5252948

Please sign in to comment.