Skip to content

Commit 10d9546

Browse files
committed
rebuild
1 parent 04bc5f7 commit 10d9546

File tree

5 files changed

+54
-31
lines changed

5 files changed

+54
-31
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ json separate from validating it, via the `cast` method.
1919
- [Usage](#usage)
2020
- [API](#api)
2121
- [`yup`](#yup)
22-
- [`.reach(schema: Schema, path: string, value: ?object, context: ?object): Schema`](#reachschema-schema-path-string-value-object-context-object-schema)
23-
- [`.addMethod(schemaType: Schema, name: string, method: ()=> Schema): void`](#addmethodschematype-schema-name-string-method--schema-void)
22+
- [`yup.reach(schema: Schema, path: string, value: ?object, context: ?object): Schema`](#yupreachschema-schema-path-string-value-object-context-object-schema)
23+
- [`yup.addMethod(schemaType: Schema, name: string, method: ()=> Schema): void`](#yupaddmethodschematype-schema-name-string-method--schema-void)
24+
- [`yup.ref(path: string, options: { contextPrefix: string }): Ref`](#yuprefpath-string-options--contextprefix-string--ref)
2425
- [`ValidationError(errors: string | Array<string>, value: any, path: string)`](#validationerrorerrors-string--arraystring-value-any-path-string)
25-
- [`ref(path: string, options: { contextPrefix: string }): Ref`](#refpath-string-options--contextprefix-string--ref)
2626
- [mixed](#mixed)
2727
- [`mixed.clone(): Schema`](#mixedclone-schema)
2828
- [`mixed.label(label: string): Schema`](#mixedlabellabel-string-schema)
@@ -33,7 +33,8 @@ json separate from validating it, via the `cast` method.
3333
- [`mixed.isValid(value: any, options: ?object, callback: ?function): Promise<boolean>`](#mixedisvalidvalue-any-options-object-callback-function-promiseboolean)
3434
- [`mixed.cast(value: any): any`](#mixedcastvalue-any-any)
3535
- [`mixed.isType(value: any): boolean`](#mixedistypevalue-any-boolean)
36-
- [`mixed.strict(isStrict: boolean = false): void`](#mixedstrictisstrict-boolean--false-void)
36+
- [`mixed.strict(isStrict: boolean = false): Schema`](#mixedstrictisstrict-boolean--false-schema)
37+
- [`mixed.strip(stripField: boolean = true): Schema`](#mixedstripstripfield-boolean--true-schema)
3738
- [`mixed.withMutation(builder: (current: Schema) => void): void`](#mixedwithmutationbuilder-current-schema--void-void)
3839
- [`mixed.default(value: any): Schema`](#mixeddefaultvalue-any-schema)
3940
- [`mixed.default(): Any`](#mixeddefault-any)
@@ -42,9 +43,9 @@ json separate from validating it, via the `cast` method.
4243
- [`mixed.typeError(message: string): Schema`](#mixedtypeerrormessage-string-schema)
4344
- [`mixed.oneOf(arrayOfValues: Array<any>, string: ?message): Schema` Alias: `equals`](#mixedoneofarrayofvalues-arrayany-string-message-schema-alias-equals)
4445
- [`mixed.notOneOf(arrayOfValues: Array<any>, string: ?message)`](#mixednotoneofarrayofvalues-arrayany-string-message)
45-
- [`mixed.when(keys: string |Array<string>, builder: object | (value, schema)=> Schema): Schema`](#mixedwhenkeys-string-arraystring-builder-object--value-schema-schema-schema)
46-
- [`mixed.test(name: string, message: string, test: function, callbackStyleAsync: ?boolean)`](#mixedtestname-string-message-string-test-function-callbackstyleasync-boolean)
47-
- [`mixed.test(options: object)`](#mixedtestoptions-object)
46+
- [`mixed.when(keys: string | Array<string>, builder: object | (value, schema)=> Schema): Schema`](#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema)
47+
- [`mixed.test(name: string, message: string, test: function, callbackStyleAsync: ?boolean): Schema`](#mixedtestname-string-message-string-test-function-callbackstyleasync-boolean-schema)
48+
- [`mixed.test(options: object): Schema`](#mixedtestoptions-object-schema)
4849
- [`mixed.transform((currentValue: any, originalValue: any) => any): Schema`](#mixedtransformcurrentvalue-any-originalvalue-any--any-schema)
4950
- [string](#string)
5051
- [`string.required(message: ?string): Schema`](#stringrequiredmessage-string-schema)
@@ -62,18 +63,18 @@ json separate from validating it, via the `cast` method.
6263
- [`number.positive(message: ?string): Schema`](#numberpositivemessage-string-schema)
6364
- [`number.negative(message: ?string): Schema`](#numbernegativemessage-string-schema)
6465
- [`number.integer(message: ?string): Schema`](#numberintegermessage-string-schema)
65-
- [`round(type: 'floor' | 'ceil' | 'round' = 'round'): Schema`](#roundtype-floor--ceil--round--round-schema)
66+
- [`number.round(type: 'floor' | 'ceil' | 'round' = 'round'): Schema`](#numberroundtype-floor--ceil--round--round-schema)
6667
- [boolean](#boolean)
6768
- [date](#date)
68-
- [`date.min(limit: Date | string | Ref, message: ?string)`](#dateminlimit-date--string--ref-message-string)
69-
- [`date.max(limit: Date | string | Ref, message: ?string)`](#datemaxlimit-date--string--ref-message-string)
69+
- [`date.min(limit: Date | string | Ref, message: ?string): Schema`](#dateminlimit-date--string--ref-message-string-schema)
70+
- [`date.max(limit: Date | string | Ref, message: ?string): Schema`](#datemaxlimit-date--string--ref-message-string-schema)
7071
- [array](#array)
71-
- [`array.of(type: Schema): Schema`](#arrayoftype-schema-schema)
72+
- [`array.of(type: Schema): Schema`](#arrayoftype-schema-schema)
7273
- [`array.required(message: ?string): Schema`](#arrayrequiredmessage-string-schema)
7374
- [`array.min(limit: number | Ref, message: ?string): Schema`](#arrayminlimit-number--ref-message-string-schema)
7475
- [`array.max(limit: number | Ref, message: ?string): Schema`](#arraymaxlimit-number--ref-message-string-schema)
75-
- [`array.ensure(): Schema`](#arrayensure-schema)
76-
- [`array.compact(rejector: (value) => boolean): Schema`](#arraycompactrejector-value--boolean-schema)
76+
- [`array.ensure(): Schema`](#arrayensure-schema)
77+
- [`array.compact(rejector: (value) => boolean): Schema`](#arraycompactrejector-value--boolean-schema)
7778
- [object](#object)
7879
- [`object.shape(fields: object, noSortEdges: ?Array<[string, string]>): Schema`](#objectshapefields-object-nosortedges-arraystring-string-schema)
7980
- [`object.from(fromKey: string, toKey: string, alias: boolean = false): Schema`](#objectfromfromkey-string-tokey-string-alias-boolean--false-schema)

lib/array.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ var hasLength = function hasLength(value) {
3030

3131
module.exports = ArraySchema;
3232

33-
function ArraySchema() {
33+
function ArraySchema(type) {
3434
var _this = this;
3535

36-
if (!(this instanceof ArraySchema)) return new ArraySchema();
36+
if (!(this instanceof ArraySchema)) return new ArraySchema(type);
3737

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

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

5050
return this.isType(values) ? values : null;
5151
});
52+
53+
if (type) _this.of(type);
5254
});
5355
}
5456

@@ -141,6 +143,11 @@ inherits(ArraySchema, MixedSchema, {
141143
}
142144
});
143145
},
146+
ensure: function ensure() {
147+
return this.default([]).transform(function (val) {
148+
return val != null ? [] : [].concat(val);
149+
});
150+
},
144151
compact: function compact(rejector) {
145152
var reject = !rejector ? function (v) {
146153
return !!v;

lib/locale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
url: '${path} must be a valid URL',
1919
trim: '${path} must be a trimmed string',
2020
lowercase: '${path} must be a lowercase string',
21-
uppercase: '${path} must be a uppercase string'
21+
uppercase: '${path} must be a upper case string'
2222
},
2323

2424
number: {

lib/mixed.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,13 @@ SchemaType.prototype = {
354354

355355
return next;
356356
},
357+
strip: function strip() {
358+
var _strip = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
359+
360+
var next = this.clone();
361+
next._strip = _strip;
362+
return next;
363+
},
357364
_option: function _option(key, overrides) {
358365
return _.has(overrides, key) ? overrides[key] : this._options[key];
359366
},

lib/object.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ function ObjectSchema(spec) {
6363
}
6464
});
6565

66+
this.fields = Object.create(null);
67+
this._nodes = [];
68+
this._excludedEdges = [];
69+
6670
this.withMutation(function () {
6771
_this2.transform(function coerce(value) {
6872
if (typeof value === 'string') {
@@ -75,13 +79,9 @@ function ObjectSchema(spec) {
7579
if (this.isType(value)) return value;
7680
return null;
7781
});
78-
});
79-
80-
this.fields = Object.create(null);
81-
this._nodes = [];
82-
this._excludedEdges = [];
8382

84-
if (spec) return this.shape(spec);
83+
if (spec) _this2.shape(spec);
84+
});
8585
}
8686

8787
inherits(ObjectSchema, MixedSchema, {
@@ -116,16 +116,13 @@ inherits(ObjectSchema, MixedSchema, {
116116

117117
if (refValue !== undefined) obj[prop] = refValue;
118118
} else if (exists && field) {
119-
// ugly optimization avoiding a clone. clears default for recursive
120-
// cast and resets it below;
121-
var hasDflt = has(schema, '_default'),
122-
dflt = schema._default;
123-
124-
var fieldSchema = childSchema(field, schema.default(undefined));
125-
126-
obj[prop] = fieldSchema.cast(value[prop], innerOptions);
119+
tempClearDefault(schema, function () {
120+
var fieldSchema = childSchema(field, schema.default(undefined));
127121

128-
if (hasDflt) schema.default(dflt);else delete schema._default;
122+
if (fieldSchema._strip !== true) {
123+
obj[prop] = fieldSchema.cast(value[prop], innerOptions);
124+
}
125+
});
129126
} else if (exists && !strip) obj[prop] = value[prop];else if (field) {
130127
var fieldDefault = field.default ? field.default() : undefined;
131128

@@ -258,6 +255,17 @@ function unknown(ctx, value) {
258255
});
259256
}
260257

258+
// ugly optimization avoiding a clone. clears default for recursive
259+
// cast and resets it below;
260+
function tempClearDefault(schema, fn) {
261+
var hasDflt = has(schema, '_default'),
262+
dflt = schema._default;
263+
264+
fn(schema);
265+
266+
if (hasDflt) schema.default(dflt);else delete schema._default;
267+
}
268+
261269
function sortFields(fields) {
262270
var excludes = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
263271

0 commit comments

Comments
 (0)