Skip to content

Commit

Permalink
rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Jun 24, 2016
1 parent 4ab2137 commit d8cdffb
Show file tree
Hide file tree
Showing 26 changed files with 1,028 additions and 400 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ json separate from validating it, via the `cast` method.
- [`string.matches(regex: Regex, message: ?string): Schema`](#stringmatchesregex-regex-message-string-schema)
- [`string.email(message: ?string): Schema`](#stringemailmessage-string-schema)
- [`string.url(message: ?string): Schema`](#stringurlmessage-string-schema)
- [`string.ensure(): Schema`](#stringensure-schema)
- [`string.trim(message: ?string): Schema`](#stringtrimmessage-string-schema)
- [`string.lowercase(message: ?string): Schema`](#stringlowercasemessage-string-schema)
- [`string.uppercase(message: ?string): Schema`](#stringuppercasemessage-string-schema)
Expand All @@ -64,7 +65,8 @@ json separate from validating it, via the `cast` method.
- [`number.positive(message: ?string): Schema`](#numberpositivemessage-string-schema)
- [`number.negative(message: ?string): Schema`](#numbernegativemessage-string-schema)
- [`number.integer(message: ?string): Schema`](#numberintegermessage-string-schema)
- [`number.round(type: 'floor' | 'ceil' | 'round' = 'round'): Schema`](#numberroundtype-floor--ceil--round--round-schema)
- [`number.truncate(): Schema`](#numbertruncate-schema)
- [`number.round(type: 'floor' | 'ceil' | 'trunc' | 'round' = 'round'): Schema`](#numberroundtype-floor--ceil--trunc--round--round-schema)
- [boolean](#boolean)
- [date](#date)
- [`date.min(limit: Date | string | Ref, message: ?string): Schema`](#dateminlimit-date--string--ref-message-string-schema)
Expand Down
78 changes: 78 additions & 0 deletions lib/Condition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

exports.__esModule = true;

var _has = require('lodash/has');

var _has2 = _interopRequireDefault(_has);

var _isSchema = require('./util/isSchema');

var _isSchema2 = _interopRequireDefault(_isSchema);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Conditional = function () {
function Conditional(refs, options) {
var _this = this;

_classCallCheck(this, Conditional);

var is = options.is;
var then = options.then;
var otherwise = options.otherwise;


this.refs = [].concat(refs);

if (typeof options === 'function') this.fn = options;else {
(function () {
if (!(0, _has2.default)(options, 'is')) throw new TypeError('`is:` is required for `when()` conditions');

if (!options.then && !options.otherwise) throw new TypeError('either `then:` or `otherwise:` is required for `when()` conditions');

var isFn = typeof is === 'function' ? is : function () {
for (var _len = arguments.length, values = Array(_len), _key = 0; _key < _len; _key++) {
values[_key] = arguments[_key];
}

return values.every(function (value) {
return value === is;
});
};

_this.fn = function () {
for (var _len2 = arguments.length, values = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
values[_key2] = arguments[_key2];
}

var ctx = values.pop();
return isFn.apply(undefined, values) ? ctx.concat(then) : ctx.concat(otherwise);
};
})();
}
}

Conditional.prototype.getValue = function getValue(parent, context) {
var values = this.refs.map(function (r) {
return r.getValue(parent, context);
});

return values;
};

Conditional.prototype.resolve = function resolve(ctx, values) {
var schema = this.fn.apply(ctx, values.concat(ctx));

if (schema !== undefined && !(0, _isSchema2.default)(schema)) throw new TypeError('conditions must return a schema object');

return schema || ctx;
};

return Conditional;
}();

exports.default = Conditional;
module.exports = exports['default'];
49 changes: 49 additions & 0 deletions lib/Lazy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

exports.__esModule = true;

var _isSchema = require('./util/isSchema');

var _isSchema2 = _interopRequireDefault(_isSchema);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Lazy = function () {
function Lazy(mapFn) {
_classCallCheck(this, Lazy);

this._resolve = function () {
var schema = mapFn.apply(undefined, arguments);
if (!(0, _isSchema2.default)(schema)) throw new TypeError('lazy() functions must return a valid schema');

return schema;
};
}

Lazy.prototype.resolve = function resolve(_ref) {
var value = _ref.value;

var rest = _objectWithoutProperties(_ref, ['value']);

return this._resolve(value, rest);
};

Lazy.prototype.cast = function cast(value, options) {
return this._resolve(value, options).cast(value, options);
};

Lazy.prototype.validate = function validate(value, options) {
return this._resolve(value, options).validate(value, options);
};

return Lazy;
}();

Lazy.prototype.__isYupSchema__ = true;

exports.default = Lazy;
module.exports = exports['default'];
62 changes: 62 additions & 0 deletions lib/Reference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

exports.__esModule = true;

var _propertyExpr = require('property-expr');

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var validateName = function validateName(d) {
if (typeof d !== 'string') throw new TypeError('ref\'s must be strings, got: ' + d);
};

var Reference = function () {
Reference.isRef = function isRef(value) {
return !!(value && (value.__isYupRef || value instanceof Reference));
};

function Reference(key, mapFn) {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

_classCallCheck(this, Reference);

validateName(key);
var prefix = options.contextPrefix || '$';

if (typeof key === 'function') {
key = '.';
}

this.key = key.trim();
this.prefix = prefix;
this.isContext = this.key.indexOf(prefix) === 0;
this.isSelf = this.key === '.';

this.path = this.isContext ? this.key.slice(this.prefix.length) : this.key;
this._get = (0, _propertyExpr.getter)(this.path, true);
this.map = mapFn || function (value) {
return value;
};
}

Reference.prototype.cast = function cast(value, _ref) {
var parent = _ref.parent;
var context = _ref.context;

return this.getValue(parent, context);
};

Reference.prototype.getValue = function getValue(parent, context) {
var isContext = this.isContext;
var value = this._get(isContext ? context : parent || context || {});
return this.map(value);
};

return Reference;
}();

exports.default = Reference;


Reference.prototype.__isYupRef = true;
module.exports = exports['default'];
61 changes: 61 additions & 0 deletions lib/ValidationError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var strReg = /\$\{\s*(\w+)\s*\}/g;

var replace = function replace(str) {
return function (params) {
return str.replace(strReg, function (_, key) {
return params[key] || '';
});
};
};

module.exports = ValidationError;

function ValidationError(errors, value, field, type) {
var _this = this;

this.name = 'ValidationError';
this.value = value;
this.path = field;
this.type = type;
this.errors = [];
this.inner = [];

if (errors) [].concat(errors).forEach(function (err) {
_this.errors = _this.errors.concat(err.errors || err);

if (err.inner) _this.inner = _this.inner.concat(err.inner.length ? err.inner : err);
});

this.message = this.errors.length > 1 ? this.errors.length + ' errors occurred' : this.errors[0];

if (Error.captureStackTrace) Error.captureStackTrace(this, ValidationError);
}

ValidationError.prototype = Object.create(Error.prototype);
ValidationError.prototype.constructor = ValidationError;

ValidationError.isError = function (err) {
return err && err.name === 'ValidationError';
};

ValidationError.formatError = function (message, params) {

if (typeof message === 'string') message = replace(message);

var fn = function fn(_ref) {
var path = _ref.path;
var label = _ref.label;

var params = _objectWithoutProperties(_ref, ['path', 'label']);

params.path = label || path || 'this';

return message(params);
};

return arguments.length === 1 ? fn : fn(params);
};
Loading

0 comments on commit d8cdffb

Please sign in to comment.