Skip to content

Commit

Permalink
rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Feb 1, 2016
1 parent 29344aa commit ae5641c
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 170 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ json separate from validating it, via the `cast` method.
- [`.reach(Schema schema, String path, Object options)`](#reachschema-schema-string-path-object-options)
- [`.addMethod(schemaType, name, method)`](#addmethodschematype-name-method)
- [`ValidationError(String|Array<String> errors, Any value, String path)`](#validationerrorstringarraystring-errors-any-value-string-path)
- [`mixed`](#mixed)
- [mixed](#mixed)
- [`mixed.clone()`](#mixedclone)
- [`mixed.concat(Schema schema)`](#mixedconcatschema-schema)
- [`mixed.validate(Any value, [Object options, Function callback])`](#mixedvalidateany-value-object-options-function-callback)
- [`mixed.isValid(Any value, [Object options, Function callback]) -> Promise`](#mixedisvalidany-value-object-options-function-callback---promise)
- [`mixed.cast(value) -> Any`](#mixedcastvalue---any)
- [`mixed.isType(Any value) -> Boolean`](#mixedistypeany-value---boolean)
- [`mixed.strict()` (default: `false`)](#mixedstrict-default-false)
- [`mixed.withMutation(Function fn)`](#mixedwithmutationfunction-fn)
- [`mixed.default(Any value)`](#mixeddefaultany-value)
- [`mixed.default() -> Any`](#mixeddefault---any)
- [`mixed.typeError(String message)` (default: '${path} (value: \`${value}\`) must be a \`${type}\` type')](#mixedtypeerrorstring-message-default-path-value-%5Cvalue%5C-must-be-a-%5Ctype%5C-type)
- [`mixed.nullable(Bool isNullable)` (default: `false`)](#mixednullablebool-isnullable-default-false)
- [`mixed.nullable(Bool isNullable = false)`](#mixednullablebool-isnullable--false)
- [`mixed.required([String message])`](#mixedrequiredstring-message)
- [`mixed.typeError(String message)`](#mixedtypeerrorstring-message)
- [`mixed.oneOf(Array<Any> arrayOfValues, [String message])` Alias: `equals`](#mixedoneofarrayany-arrayofvalues-string-message-alias-equals)
- [`mixed.notOneOf(Array<Any> arrayOfValues, [String message])`](#mixednotoneofarrayany-arrayofvalues-string-message)
- [`mixed.when(String key, Object options | Function func)`](#mixedwhenstring-key-object-options--function-func)
Expand Down
44 changes: 32 additions & 12 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument

var MixedSchema = require('./mixed');
var Promise = require('promise/lib/es6-extensions');
var isAbsent = require('./util/isAbsent');

var _require = require('./locale.js');

Expand All @@ -22,23 +23,29 @@ var scopeError = function scopeError(value) {
};
};

var hasLength = function hasLength(value) {
return !isAbsent(value) && value.length > 0;
};

module.exports = ArraySchema;

function ArraySchema() {
var _this = this;

if (!(this instanceof ArraySchema)) return new ArraySchema();

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

this.transforms.push(function (values) {
if (typeof values === 'string') try {
values = JSON.parse(values);
} catch (err) {
values = null;
}

if (Array.isArray(values)) return this._subType ? values.map(this._subType.cast, this._subType) : values;
this.withMutation(function () {
_this.transform(function (values) {
if (typeof values === 'string') try {
values = JSON.parse(values);
} catch (err) {
values = null;
}

return this.isType(values) ? values : null;
return this.isType(values) ? values : null;
});
});
}

Expand All @@ -48,6 +55,19 @@ inherits(ArraySchema, MixedSchema, {
return Array.isArray(v);
},

_cast: function _cast(_value, _opts) {
var _this2 = this;

var value = MixedSchema.prototype._cast.call(this, _value);

//should ignore nulls here
if (!this._typeCheck(value) || !this._subType) return value;

return value.map(function (v) {
return _this2._subType.cast(v, _opts);
});
},

_validate: function _validate(_value, _opts, _state) {
var errors = [],
context,
Expand Down Expand Up @@ -96,7 +116,7 @@ inherits(ArraySchema, MixedSchema, {
required: function required(msg) {
var next = MixedSchema.prototype.required.call(this, msg || mixed.required);

return next.min(1, msg || mixed.required);
return next.test('required', msg || mixed.required, hasLength);
},

min: function min(_min, message) {
Expand All @@ -108,7 +128,7 @@ inherits(ArraySchema, MixedSchema, {
exclusive: true,
params: { min: _min },
test: function test(value) {
return value && value.length >= _min;
return isAbsent(value) || value.length >= _min;
}
});
},
Expand All @@ -121,7 +141,7 @@ inherits(ArraySchema, MixedSchema, {
exclusive: true,
params: { max: _max },
test: function test(value) {
return value && value.length <= _max;
return isAbsent(value) || value.length <= _max;
}
});
},
Expand Down
12 changes: 8 additions & 4 deletions lib/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ var MixedSchema = require('./mixed'),
module.exports = BooleanSchema;

function BooleanSchema() {
var _this = this;

if (!(this instanceof BooleanSchema)) return new BooleanSchema();

MixedSchema.call(this, { type: 'boolean' });

this.transforms.push(function (value) {
if (this.isType(value)) return value;
return (/true|1/i.test(value)
);
this.withMutation(function () {
_this.transform(function (value) {
if (this.isType(value)) return value;
return (/true|1/i.test(value)
);
});
});
}

Expand Down
17 changes: 11 additions & 6 deletions lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var MixedSchema = require('./mixed');
var isoParse = require('./util/isodate');
var locale = require('./locale.js').date;
var isAbsent = require('./util/isAbsent');

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

Expand All @@ -13,15 +14,19 @@ var invalidDate = new Date('');
module.exports = DateSchema;

function DateSchema() {
var _this = this;

if (!(this instanceof DateSchema)) return new DateSchema();

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

this.transforms.push(function (value) {
if (this.isType(value)) return isDate(value) ? new Date(value) : value;
this.withMutation(function () {
_this.transform(function (value) {
if (this.isType(value)) return isDate(value) ? new Date(value) : value;

value = isoParse(value);
return value ? new Date(value) : invalidDate;
value = isoParse(value);
return value ? new Date(value) : invalidDate;
});
});
}

Expand All @@ -42,7 +47,7 @@ inherits(DateSchema, MixedSchema, {
message: msg || locale.min,
params: { min: _min },
test: function test(value) {
return value && value >= limit;
return isAbsent(value) || value >= limit;
}
});
},
Expand All @@ -58,7 +63,7 @@ inherits(DateSchema, MixedSchema, {
message: msg || locale.max,
params: { max: _max },
test: function test(value) {
return !value || value <= limit;
return isAbsent(value) || value <= limit;
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
boolean: {},

object: {
noUnknown: '${path} field cannot have keys not specified in the objcet shape'
noUnknown: '${path} field cannot have keys not specified in the object shape'
},

array: {
Expand Down
Loading

0 comments on commit ae5641c

Please sign in to comment.