From 10d9546c29023a6d561b4830dd2f4541c74791a3 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Tue, 12 Apr 2016 09:37:44 -0400 Subject: [PATCH] rebuild --- README.md | 27 ++++++++++++++------------- lib/array.js | 11 +++++++++-- lib/locale.js | 2 +- lib/mixed.js | 7 +++++++ lib/object.js | 38 +++++++++++++++++++++++--------------- 5 files changed, 54 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 7b35b1bfe..1131ba577 100644 --- a/README.md +++ b/README.md @@ -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, 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) @@ -33,7 +33,8 @@ json separate from validating it, via the `cast` method. - [`mixed.isValid(value: any, options: ?object, callback: ?function): Promise`](#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) @@ -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, string: ?message): Schema` Alias: `equals`](#mixedoneofarrayofvalues-arrayany-string-message-schema-alias-equals) - [`mixed.notOneOf(arrayOfValues: Array, string: ?message)`](#mixednotoneofarrayofvalues-arrayany-string-message) - - [`mixed.when(keys: string |Array, 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, 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) @@ -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) diff --git a/lib/array.js b/lib/array.js index a2b147224..3c90349aa 100644 --- a/lib/array.js +++ b/lib/array.js @@ -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' }); @@ -49,6 +49,8 @@ function ArraySchema() { return this.isType(values) ? values : null; }); + + if (type) _this.of(type); }); } @@ -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; diff --git a/lib/locale.js b/lib/locale.js index e011095df..43aa37c44 100644 --- a/lib/locale.js +++ b/lib/locale.js @@ -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: { diff --git a/lib/mixed.js b/lib/mixed.js index a92d4ac4d..d580cd5d5 100644 --- a/lib/mixed.js +++ b/lib/mixed.js @@ -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]; }, diff --git a/lib/object.js b/lib/object.js index 7b423a784..ec91c7d92 100644 --- a/lib/object.js +++ b/lib/object.js @@ -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') { @@ -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, { @@ -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; @@ -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];