Skip to content

Commit

Permalink
rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 12, 2016
1 parent 04bc5f7 commit 10d9546
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ json separate from validating it, via the `cast` method.
- [Usage](#usage)
- [API](#api)
- [`yup`](#yup)
- [`.reach(schema: Schema, path: string, value: ?object, context: ?object): Schema`](#reachschema-schema-path-string-value-object-context-object-schema)
- [`.addMethod(schemaType: Schema, name: string, method: ()=> Schema): void`](#addmethodschematype-schema-name-string-method--schema-void)
- [`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)
- [`ValidationError(errors: string | Array<string>, value: any, path: string)`](#validationerrorerrors-string--arraystring-value-any-path-string)
- [`ref(path: string, options: { contextPrefix: string }): Ref`](#refpath-string-options--contextprefix-string--ref)
- [mixed](#mixed)
- [`mixed.clone(): Schema`](#mixedclone-schema)
- [`mixed.label(label: string): Schema`](#mixedlabellabel-string-schema)
Expand All @@ -33,7 +33,8 @@ json separate from validating it, via the `cast` method.
- [`mixed.isValid(value: any, options: ?object, callback: ?function): Promise<boolean>`](#mixedisvalidvalue-any-options-object-callback-function-promiseboolean)
- [`mixed.cast(value: any): any`](#mixedcastvalue-any-any)
- [`mixed.isType(value: any): boolean`](#mixedistypevalue-any-boolean)
- [`mixed.strict(isStrict: boolean = false): void`](#mixedstrictisstrict-boolean--false-void)
- [`mixed.strict(isStrict: boolean = false): Schema`](#mixedstrictisstrict-boolean--false-schema)
- [`mixed.strip(stripField: boolean = true): Schema`](#mixedstripstripfield-boolean--true-schema)
- [`mixed.withMutation(builder: (current: Schema) => void): void`](#mixedwithmutationbuilder-current-schema--void-void)
- [`mixed.default(value: any): Schema`](#mixeddefaultvalue-any-schema)
- [`mixed.default(): Any`](#mixeddefault-any)
Expand All @@ -42,9 +43,9 @@ json separate from validating it, via the `cast` method.
- [`mixed.typeError(message: string): Schema`](#mixedtypeerrormessage-string-schema)
- [`mixed.oneOf(arrayOfValues: Array<any>, string: ?message): Schema` Alias: `equals`](#mixedoneofarrayofvalues-arrayany-string-message-schema-alias-equals)
- [`mixed.notOneOf(arrayOfValues: Array<any>, string: ?message)`](#mixednotoneofarrayofvalues-arrayany-string-message)
- [`mixed.when(keys: string |Array<string>, builder: object | (value, schema)=> Schema): Schema`](#mixedwhenkeys-string-arraystring-builder-object--value-schema-schema-schema)
- [`mixed.test(name: string, message: string, test: function, callbackStyleAsync: ?boolean)`](#mixedtestname-string-message-string-test-function-callbackstyleasync-boolean)
- [`mixed.test(options: object)`](#mixedtestoptions-object)
- [`mixed.when(keys: string | Array<string>, builder: object | (value, schema)=> Schema): Schema`](#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema)
- [`mixed.test(name: string, message: string, test: function, callbackStyleAsync: ?boolean): Schema`](#mixedtestname-string-message-string-test-function-callbackstyleasync-boolean-schema)
- [`mixed.test(options: object): Schema`](#mixedtestoptions-object-schema)
- [`mixed.transform((currentValue: any, originalValue: any) => any): Schema`](#mixedtransformcurrentvalue-any-originalvalue-any--any-schema)
- [string](#string)
- [`string.required(message: ?string): Schema`](#stringrequiredmessage-string-schema)
Expand All @@ -62,18 +63,18 @@ json separate from validating it, via the `cast` method.
- [`number.positive(message: ?string): Schema`](#numberpositivemessage-string-schema)
- [`number.negative(message: ?string): Schema`](#numbernegativemessage-string-schema)
- [`number.integer(message: ?string): Schema`](#numberintegermessage-string-schema)
- [`round(type: 'floor' | 'ceil' | 'round' = 'round'): Schema`](#roundtype-floor--ceil--round--round-schema)
- [`number.round(type: 'floor' | 'ceil' | 'round' = 'round'): Schema`](#numberroundtype-floor--ceil--round--round-schema)
- [boolean](#boolean)
- [date](#date)
- [`date.min(limit: Date | string | Ref, message: ?string)`](#dateminlimit-date--string--ref-message-string)
- [`date.max(limit: Date | string | Ref, message: ?string)`](#datemaxlimit-date--string--ref-message-string)
- [`date.min(limit: Date | string | Ref, message: ?string): Schema`](#dateminlimit-date--string--ref-message-string-schema)
- [`date.max(limit: Date | string | Ref, message: ?string): Schema`](#datemaxlimit-date--string--ref-message-string-schema)
- [array](#array)
- [`array.of(type: Schema): Schema`](#arrayoftype-schema-schema)
- [`array.of(type: Schema): Schema`](#arrayoftype-schema-schema)
- [`array.required(message: ?string): Schema`](#arrayrequiredmessage-string-schema)
- [`array.min(limit: number | Ref, message: ?string): Schema`](#arrayminlimit-number--ref-message-string-schema)
- [`array.max(limit: number | Ref, message: ?string): Schema`](#arraymaxlimit-number--ref-message-string-schema)
- [`array.ensure(): Schema`](#arrayensure-schema)
- [`array.compact(rejector: (value) => boolean): Schema`](#arraycompactrejector-value--boolean-schema)
- [`array.ensure(): Schema`](#arrayensure-schema)
- [`array.compact(rejector: (value) => boolean): Schema`](#arraycompactrejector-value--boolean-schema)
- [object](#object)
- [`object.shape(fields: object, noSortEdges: ?Array<[string, string]>): Schema`](#objectshapefields-object-nosortedges-arraystring-string-schema)
- [`object.from(fromKey: string, toKey: string, alias: boolean = false): Schema`](#objectfromfromkey-string-tokey-string-alias-boolean--false-schema)
Expand Down
11 changes: 9 additions & 2 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ var hasLength = function hasLength(value) {

module.exports = ArraySchema;

function ArraySchema() {
function ArraySchema(type) {
var _this = this;

if (!(this instanceof ArraySchema)) return new ArraySchema();
if (!(this instanceof ArraySchema)) return new ArraySchema(type);

MixedSchema.call(this, { type: 'array' });

Expand All @@ -49,6 +49,8 @@ function ArraySchema() {

return this.isType(values) ? values : null;
});

if (type) _this.of(type);
});
}

Expand Down Expand Up @@ -141,6 +143,11 @@ inherits(ArraySchema, MixedSchema, {
}
});
},
ensure: function ensure() {
return this.default([]).transform(function (val) {
return val != null ? [] : [].concat(val);
});
},
compact: function compact(rejector) {
var reject = !rejector ? function (v) {
return !!v;
Expand Down
2 changes: 1 addition & 1 deletion lib/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
url: '${path} must be a valid URL',
trim: '${path} must be a trimmed string',
lowercase: '${path} must be a lowercase string',
uppercase: '${path} must be a uppercase string'
uppercase: '${path} must be a upper case string'
},

number: {
Expand Down
7 changes: 7 additions & 0 deletions lib/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ SchemaType.prototype = {

return next;
},
strip: function strip() {
var _strip = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];

var next = this.clone();
next._strip = _strip;
return next;
},
_option: function _option(key, overrides) {
return _.has(overrides, key) ? overrides[key] : this._options[key];
},
Expand Down
38 changes: 23 additions & 15 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ function ObjectSchema(spec) {
}
});

this.fields = Object.create(null);
this._nodes = [];
this._excludedEdges = [];

this.withMutation(function () {
_this2.transform(function coerce(value) {
if (typeof value === 'string') {
Expand All @@ -75,13 +79,9 @@ function ObjectSchema(spec) {
if (this.isType(value)) return value;
return null;
});
});

this.fields = Object.create(null);
this._nodes = [];
this._excludedEdges = [];

if (spec) return this.shape(spec);
if (spec) _this2.shape(spec);
});
}

inherits(ObjectSchema, MixedSchema, {
Expand Down Expand Up @@ -116,16 +116,13 @@ inherits(ObjectSchema, MixedSchema, {

if (refValue !== undefined) obj[prop] = refValue;
} else if (exists && field) {
// ugly optimization avoiding a clone. clears default for recursive
// cast and resets it below;
var hasDflt = has(schema, '_default'),
dflt = schema._default;

var fieldSchema = childSchema(field, schema.default(undefined));

obj[prop] = fieldSchema.cast(value[prop], innerOptions);
tempClearDefault(schema, function () {
var fieldSchema = childSchema(field, schema.default(undefined));

if (hasDflt) schema.default(dflt);else delete schema._default;
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;

Expand Down Expand Up @@ -258,6 +255,17 @@ 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 sortFields(fields) {
var excludes = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];

Expand Down

0 comments on commit 10d9546

Please sign in to comment.