-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,028 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
Oops, something went wrong.