Skip to content

Commit

Permalink
[added] strip() method for objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 12, 2016
1 parent 00f85b8 commit c3b613b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 22 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ You should use `isType` for all Schema type checks.
Sets the `strict` option to `true`. Strict schemas skip coercion and transformation attempts,
validating the value "as is".

#### `mixed.strip(stripField: boolean = true): Schema`

Marks a schema to be removed from an output object. Only works as a nested schema.

```js
let schema = object({
useThis: number(),
notThis: string().strip()
})

schema.cast({ notThis: 'foo', useThis: 4 }) // { useThis: 4 }
```
#### `mixed.withMutation(builder: (current: Schema) => void): void`
First the legally required Rich Hickey quote:
Expand Down Expand Up @@ -817,6 +830,19 @@ yup.object().shape({
})
```
You can also pass a shape to the object constructor as a convenience.
```js
object().shape({
num: number()
})
//or
object({
num: number()
})
```
#### `object.shape(fields: object, noSortEdges: ?Array<[string, string]>): Schema`
Define the keys of the object and the schemas for said keys.
Expand Down
15 changes: 11 additions & 4 deletions lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var MixedSchema = require('./mixed');
var isoParse = require('./util/isodate');
var locale = require('./locale.js').date;
var isAbsent = require('./util/isAbsent');
var Ref = require('./util/reference');

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

Expand Down Expand Up @@ -37,9 +38,12 @@ inherits(DateSchema, MixedSchema, {
return isDate(v) && !isNaN(v.getTime());
},
min: function min(_min, msg) {
var limit = this.cast(_min);
var limit = _min;

if (!this._typeCheck(limit)) throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date');
if (!Ref.isRef(limit)) {
limit = this.cast(_min);
if (!this._typeCheck(limit)) throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date');
}

return this.test({
name: 'min',
Expand All @@ -52,9 +56,12 @@ inherits(DateSchema, MixedSchema, {
});
},
max: function max(_max, msg) {
var limit = this.cast(_max);
var limit = _max;

if (!this._typeCheck(limit)) throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date');
if (!Ref.isRef(limit)) {
limit = this.cast(_max);
if (!this._typeCheck(limit)) throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date');
}

return this.test({
name: 'max',
Expand Down
2 changes: 1 addition & 1 deletion src/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
6 changes: 6 additions & 0 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ SchemaType.prototype = {
return next
},

strip(strip = true) {
let next = this.clone()
next._strip = strip
return next
},

_option(key, overrides){
return _.has(overrides, key)
? overrides[key] : this._options[key]
Expand Down
42 changes: 25 additions & 17 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ function ObjectSchema(spec) {
}
})

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

this.withMutation(() => {
this.transform(function coerce(value) {
if (typeof value === 'string') {
Expand All @@ -61,14 +65,10 @@ function ObjectSchema(spec) {
return value
return null
})
})

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

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

inherits(ObjectSchema, MixedSchema, {
Expand Down Expand Up @@ -106,17 +106,13 @@ inherits(ObjectSchema, MixedSchema, {
obj[prop] = refValue
}
else if (exists && field) {
// ugly optimization avoiding a clone. clears default for recursive
// cast and resets it below;
let hasDflt = has(schema, '_default')
, dflt = schema._default;

let fieldSchema = childSchema(field, schema.default(undefined))

obj[prop] = fieldSchema.cast(value[prop], innerOptions)
tempClearDefault(schema, () => {
let 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]
Expand Down Expand Up @@ -253,6 +249,18 @@ function unknown(ctx, value) {
.filter(key => known.indexOf(key) === -1)
}

// ugly optimization avoiding a clone. clears default for recursive
// cast and resets it below;
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, excludes = []){
var edges = [], nodes = []

Expand Down
12 changes: 12 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ describe('Object types', function(){
])
})

it('should strip specific fields', function(){
var inst = object().shape({
prop: mixed().strip(false),
other: mixed().strip()
})

inst.cast({ other: 'boo', prop: 'bar'})
.should.eql({
prop: 'bar'
})
})

it('should handle custom validation', function(){
var inst = object().shape({
prop: mixed(),
Expand Down

0 comments on commit c3b613b

Please sign in to comment.