Skip to content

Commit 31f9c28

Browse files
committed
Use withMutation to set up initial transforms
1 parent 0a7b2d4 commit 31f9c28

File tree

6 files changed

+52
-45
lines changed

6 files changed

+52
-45
lines changed

src/array.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ function ArraySchema(){
2020

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

23-
this.transforms.push(function(values) {
24-
if (typeof values === 'string')
25-
try {
26-
values = JSON.parse(values)
27-
} catch (err){ values = null }
28-
29-
if (Array.isArray(values))
30-
return this._subType
31-
? values.map(this._subType.cast, this._subType)
32-
: values
33-
34-
return this.isType(values) ? values : null
23+
this.withMutation(() => {
24+
this.transform(function(values) {
25+
if (typeof values === 'string')
26+
try {
27+
values = JSON.parse(values)
28+
} catch (err){ values = null }
29+
30+
if (Array.isArray(values))
31+
return this._subType
32+
? values.map(this._subType.cast, this._subType)
33+
: values
34+
35+
return this.isType(values) ? values : null
36+
})
3537
})
3638
}
3739

src/boolean.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ var MixedSchema = require('./mixed')
55
module.exports = BooleanSchema
66

77
function BooleanSchema(){
8-
if (!(this instanceof BooleanSchema))
8+
if (!(this instanceof BooleanSchema))
99
return new BooleanSchema()
10-
10+
1111
MixedSchema.call(this, { type: 'boolean'})
1212

13-
this.transforms.push(function(value) {
14-
if ( this.isType(value) ) return value
15-
return (/true|1/i).test(value)
13+
this.withMutation(() => {
14+
this.transform(function(value) {
15+
if ( this.isType(value) ) return value
16+
return (/true|1/i).test(value)
17+
})
1618
})
1719
}
1820

1921
inherits(BooleanSchema, MixedSchema, {
2022

21-
_typeCheck(v){
23+
_typeCheck(v){
2224
return (typeof v === 'boolean') || (typeof v === 'object' && v instanceof Boolean)
2325
}
2426
})
25-

src/date.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ function DateSchema(){
1414

1515
MixedSchema.call(this, { type: 'date'})
1616

17-
this.transforms.push(function(value) {
18-
if (this.isType(value))
19-
return isDate(value) ? new Date(value) : value
17+
this.withMutation(() => {
18+
this.transform(function(value) {
19+
if (this.isType(value))
20+
return isDate(value) ? new Date(value) : value
2021

21-
value = isoParse(value)
22-
return value ? new Date(value) : invalidDate
22+
value = isoParse(value)
23+
return value ? new Date(value) : invalidDate
24+
})
2325
})
2426
}
2527

src/number.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ function NumberSchema(){
1414

1515
SchemaObject.call(this, { type: 'number' })
1616

17-
this.transforms.push(function(value) {
18-
if ( this.isType(value) ) return value
19-
if ( typeof value === 'boolean' ) return value ? 1 : 0
17+
this.withMutation(() => {
18+
this.transform(function(value) {
19+
if (this.isType(value)) return value
20+
if (typeof value === 'boolean') return value ? 1 : 0
2021

21-
return isDate(value) ? +value : parseFloat(value)
22+
return isDate(value) ? +value : parseFloat(value)
23+
})
2224
})
2325
}
2426

src/object.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22
var MixedSchema = require('./mixed')
33
, Promise = require('promise/lib/es6-extensions')
4-
//, Reference = require('./util/Reference')
5-
, cloneDeep = require('./util/clone')
64
, toposort = require('toposort')
75
, locale = require('./locale.js').object
86
, split = require('property-expr').split
@@ -45,18 +43,18 @@ function ObjectSchema(spec) {
4543
}
4644
})
4745

48-
this.transforms.push(function coerce(value) {
49-
if (typeof value === 'string') {
50-
try {
51-
value = JSON.parse(value)
46+
this.withMutation(() => {
47+
this.transform(function coerce(value) {
48+
if (typeof value === 'string') {
49+
try {
50+
value = JSON.parse(value)
51+
}
52+
catch (err){ value = null }
5253
}
53-
catch (err){ value = null }
54-
}
55-
56-
if( this.isType(value) )
57-
return value
58-
59-
return null
54+
if (this.isType(value))
55+
return value
56+
return null
57+
})
6058
})
6159

6260
this.fields = Object.create(null)

src/string.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ function StringSchema(){
1818

1919
MixedSchema.call(this, { type: 'string'})
2020

21-
this.transforms.push(function(value) {
22-
if (this.isType(value)) return value
23-
return value == null ? ''
24-
: value.toString ? value.toString() : '' + value
21+
this.withMutation(() => {
22+
this.transform(function(value) {
23+
if (this.isType(value)) return value
24+
return value == null ? ''
25+
: value.toString ? value.toString() : '' + value
26+
})
2527
})
2628
}
2729

0 commit comments

Comments
 (0)