schema.js 5.6 KB

1
  1. "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=void 0;var _dot=_interopRequireDefault(require("@d3v4pp/dot")),_typecast=_interopRequireDefault(require("./typecast")),_property=_interopRequireDefault(require("./property")),_messages=_interopRequireDefault(require("./messages")),_validators=_interopRequireDefault(require("./validators")),_error=_interopRequireDefault(require("./error")),_utils=require("./utils");function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_unsupportedIterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArray(a){if("undefined"!=typeof Symbol&&null!=a[Symbol.iterator]||null!=a["@@iterator"])return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a))return _arrayLikeToArray(a)}function _slicedToArray(a,b){return _arrayWithHoles(a)||_iterableToArrayLimit(a,b)||_unsupportedIterableToArray(a,b)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function _iterableToArrayLimit(a,b){var c=null==a?null:"undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(null!=c){var d,e,f=[],g=!0,h=!1;try{for(c=c.call(a);!(g=(d=c.next()).done)&&(f.push(d.value),!(b&&f.length===b));g=!0);}catch(a){h=!0,e=a}finally{try{g||null==c["return"]||c["return"]()}finally{if(h)throw e}}return f}}function _arrayWithHoles(a){if(Array.isArray(a))return a}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}var Schema=function(){function a(){var b=this,c=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},d=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,a),this.opts=d,this.hooks=[],this.props={},this.messages=Object.assign({},_messages["default"]),this.validators=Object.assign({},_validators["default"]),this.typecasters=Object.assign({},_typecast["default"]),Object.keys(c).forEach(function(a){return b.path(a,c[a])})}return _createClass(a,[{key:"path",value:function path(b,c){var d=this,e=b.split("."),f=e.pop(),g=e.join(".");if(g&&this.path(g),"$"===f&&this.path(g).type(Array),c instanceof a)return c.hook(function(a,c){return d.path((0,_utils.join)(a,b),c)}),this.path(b,c.props);if(c instanceof _property["default"])return this.props[b]=c,this.propagate(b,c),c;var h=this.props[b]||new _property["default"](b,this);if(this.props[b]=h,this.propagate(b,h),!c)return h;if("string"==typeof c||"function"==typeof c)return h.type(c),h;if(Array.isArray(c))return h.type(Array),1===c.length?h.each(c[0]):h.elements(c),h;for(var i,j=Object.keys(c),k=!1,l=0,m=j;l<m.length;l++)if(i=m[l],"function"!=typeof h[i]){h.type(Object),k=!0;break}return j.forEach(function(a){var e=c[a];return k?d.path((0,_utils.join)(a,b),e):void h[a](e)}),h}},{key:"typecast",value:function typecast(a){for(var b=function _loop(){var b=_slicedToArray(d[c],2),e=b[0],f=b[1];(0,_utils.enumerate)(e,a,function(b,c){if(null!=c){var d=f.typecast(c);d===c||_dot["default"].set(a,b,d)}})},c=0,d=Object.entries(this.props);c<d.length;c++)b();return this}},{key:"strip",value:function strip(a){var b=this;return(0,_utils.walk)(a,function(c,d){return!!b.props[d]||(_dot["default"]["delete"](a,c),!1)}),this}},{key:"enforce",value:function enforce(a){var b=this,c=[];return(0,_utils.walk)(a,function(a,d){if(b.props[d])return!0;var e=new _error["default"](_messages["default"].illegal(a),a);return c.push(e),!1}),c}},{key:"validate",value:function validate(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};b=Object.assign(this.opts,b);var c=[];b.typecast&&this.typecast(a),b.strict&&c.push.apply(c,_toConsumableArray(this.enforce(a))),!1!==b.strip&&this.strip(a);for(var d=function _loop2(){var b=_slicedToArray(f[e],2),d=b[0],g=b[1];(0,_utils.enumerate)(d,a,function(b,d){var e=g.validate(d,a,b);e&&c.push(e)})},e=0,f=Object.entries(this.props);e<f.length;e++)d();return c}},{key:"assert",value:function assert(a,b){var c=this.validate(a,b),d=_slicedToArray(c,1),e=d[0];if(e)throw e}},{key:"message",value:function message(a,b){return(0,_utils.assign)(a,b,this.messages),this}},{key:"validator",value:function validator(a,b){return(0,_utils.assign)(a,b,this.validators),this}},{key:"typecaster",value:function typecaster(a,b){return(0,_utils.assign)(a,b,this.typecasters),this}},{key:"hook",value:function hook(a){return this.hooks.push(a),this}},{key:"propagate",value:function propagate(a,b){return this.hooks.forEach(function(c){return c(a,b)}),this}}]),a}();exports["default"]=Schema,Schema.ValidationError=_error["default"],module.exports=exports.default;