Skip to content

Commit c3b613b

Browse files
committed
[added] strip() method for objects
1 parent 00f85b8 commit c3b613b

File tree

6 files changed

+81
-22
lines changed

6 files changed

+81
-22
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ You should use `isType` for all Schema type checks.
332332
Sets the `strict` option to `true`. Strict schemas skip coercion and transformation attempts,
333333
validating the value "as is".
334334

335+
#### `mixed.strip(stripField: boolean = true): Schema`
336+
337+
Marks a schema to be removed from an output object. Only works as a nested schema.
338+
339+
```js
340+
let schema = object({
341+
useThis: number(),
342+
notThis: string().strip()
343+
})
344+
345+
schema.cast({ notThis: 'foo', useThis: 4 }) // { useThis: 4 }
346+
```
347+
335348
#### `mixed.withMutation(builder: (current: Schema) => void): void`
336349
337350
First the legally required Rich Hickey quote:
@@ -817,6 +830,19 @@ yup.object().shape({
817830
})
818831
```
819832
833+
You can also pass a shape to the object constructor as a convenience.
834+
835+
```js
836+
object().shape({
837+
num: number()
838+
})
839+
//or
840+
object({
841+
num: number()
842+
})
843+
```
844+
845+
820846
#### `object.shape(fields: object, noSortEdges: ?Array<[string, string]>): Schema`
821847
822848
Define the keys of the object and the schemas for said keys.

lib/date.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var MixedSchema = require('./mixed');
44
var isoParse = require('./util/isodate');
55
var locale = require('./locale.js').date;
66
var isAbsent = require('./util/isAbsent');
7+
var Ref = require('./util/reference');
78

89
var _require = require('./util/_');
910

@@ -37,9 +38,12 @@ inherits(DateSchema, MixedSchema, {
3738
return isDate(v) && !isNaN(v.getTime());
3839
},
3940
min: function min(_min, msg) {
40-
var limit = this.cast(_min);
41+
var limit = _min;
4142

42-
if (!this._typeCheck(limit)) throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date');
43+
if (!Ref.isRef(limit)) {
44+
limit = this.cast(_min);
45+
if (!this._typeCheck(limit)) throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date');
46+
}
4347

4448
return this.test({
4549
name: 'min',
@@ -52,9 +56,12 @@ inherits(DateSchema, MixedSchema, {
5256
});
5357
},
5458
max: function max(_max, msg) {
55-
var limit = this.cast(_max);
59+
var limit = _max;
5660

57-
if (!this._typeCheck(limit)) throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date');
61+
if (!Ref.isRef(limit)) {
62+
limit = this.cast(_max);
63+
if (!this._typeCheck(limit)) throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date');
64+
}
5865

5966
return this.test({
6067
name: 'max',

src/locale.js

+1-1
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: {

src/mixed.js

+6
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ SchemaType.prototype = {
382382
return next
383383
},
384384

385+
strip(strip = true) {
386+
let next = this.clone()
387+
next._strip = strip
388+
return next
389+
},
390+
385391
_option(key, overrides){
386392
return _.has(overrides, key)
387393
? overrides[key] : this._options[key]

src/object.js

+25-17
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ function ObjectSchema(spec) {
4949
}
5050
})
5151

52+
this.fields = Object.create(null)
53+
this._nodes = []
54+
this._excludedEdges = []
55+
5256
this.withMutation(() => {
5357
this.transform(function coerce(value) {
5458
if (typeof value === 'string') {
@@ -61,14 +65,10 @@ function ObjectSchema(spec) {
6165
return value
6266
return null
6367
})
64-
})
65-
66-
this.fields = Object.create(null)
67-
this._nodes = []
68-
this._excludedEdges = []
6968

70-
if ( spec )
71-
return this.shape(spec);
69+
if (spec)
70+
this.shape(spec);
71+
})
7272
}
7373

7474
inherits(ObjectSchema, MixedSchema, {
@@ -106,17 +106,13 @@ inherits(ObjectSchema, MixedSchema, {
106106
obj[prop] = refValue
107107
}
108108
else if (exists && field) {
109-
// ugly optimization avoiding a clone. clears default for recursive
110-
// cast and resets it below;
111-
let hasDflt = has(schema, '_default')
112-
, dflt = schema._default;
113-
114-
let fieldSchema = childSchema(field, schema.default(undefined))
115-
116-
obj[prop] = fieldSchema.cast(value[prop], innerOptions)
109+
tempClearDefault(schema, () => {
110+
let fieldSchema = childSchema(field, schema.default(undefined))
117111

118-
if (hasDflt) schema.default(dflt)
119-
else delete schema._default
112+
if (fieldSchema._strip !== true) {
113+
obj[prop] = fieldSchema.cast(value[prop], innerOptions)
114+
}
115+
})
120116
}
121117
else if (exists && !strip)
122118
obj[prop] = value[prop]
@@ -253,6 +249,18 @@ function unknown(ctx, value) {
253249
.filter(key => known.indexOf(key) === -1)
254250
}
255251

252+
// ugly optimization avoiding a clone. clears default for recursive
253+
// cast and resets it below;
254+
function tempClearDefault(schema, fn) {
255+
let hasDflt = has(schema, '_default')
256+
, dflt = schema._default;
257+
258+
fn(schema)
259+
260+
if (hasDflt) schema.default(dflt)
261+
else delete schema._default
262+
}
263+
256264
function sortFields(fields, excludes = []){
257265
var edges = [], nodes = []
258266

test/object.js

+12
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ describe('Object types', function(){
229229
])
230230
})
231231

232+
it('should strip specific fields', function(){
233+
var inst = object().shape({
234+
prop: mixed().strip(false),
235+
other: mixed().strip()
236+
})
237+
238+
inst.cast({ other: 'boo', prop: 'bar'})
239+
.should.eql({
240+
prop: 'bar'
241+
})
242+
})
243+
232244
it('should handle custom validation', function(){
233245
var inst = object().shape({
234246
prop: mixed(),

0 commit comments

Comments
 (0)