property.js 5.4 KB

1
  1. "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=void 0;var _error2=_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 Property=function(){function a(b,c){_classCallCheck(this,a),this.name=b,this.registry={},this._schema=c,this._type=null,this.messages={}}return _createClass(a,[{key:"message",value:function message(a){"string"==typeof a&&(a={default:a});for(var b=Object.entries(a),c=0,d=b;c<d.length;c++){var e=_slicedToArray(d[c],2),f=e[0],g=e[1];this.messages[f]=g}return this}},{key:"schema",value:function schema(a){return this._schema.path(this.name,a),this}},{key:"use",value:function use(a){var b=this;return Object.keys(a).forEach(function(c){var d=a[c];Array.isArray(d)||(d=[d]);var e=d.shift();b._register(c,d,e)}),this}},{key:"required",value:function required(){var a=!(0<arguments.length&&void 0!==arguments[0])||arguments[0];return this._register("required",[a])}},{key:"type",value:function type(a){return this._type=a,this._register("type",[a])}},{key:"string",value:function string(){return this.type(String)}},{key:"number",value:function number(){return this.type(Number)}},{key:"array",value:function array(){return this.type(Array)}},{key:"date",value:function date(){return this.type(Date)}},{key:"length",value:function length(a){return this._register("length",[a])}},{key:"size",value:function size(a){return this._register("size",[a])}},{key:"enum",value:function _enum(a){return this._register("enum",[a])}},{key:"match",value:function match(a){return this._register("match",[a])}},{key:"each",value:function each(a){return this._schema.path((0,_utils.join)("$",this.name),a),this}},{key:"elements",value:function elements(a){var b=this;return a.forEach(function(a,c){b._schema.path((0,_utils.join)(c,b.name),a)}),this}},{key:"properties",value:function properties(a){for(var b=0,c=Object.entries(a);b<c.length;b++){var d=_slicedToArray(c[b],2),e=d[0],f=d[1];this._schema.path((0,_utils.join)(e,this.name),f)}return this}},{key:"path",value:function path(){var a;return(a=this._schema).path.apply(a,arguments)}},{key:"typecast",value:function typecast(a){var b=this._schema,c=this._type;if(!c)return a;"function"==typeof c&&(c=c.name);var d=b.typecasters[c]||b.typecasters[c.toLowerCase()];if("function"!=typeof d)throw new Error("Typecasting failed: No typecaster defined for ".concat(c,"."));return d(a)}},{key:"validate",value:function validate(a,b){for(var c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:this.name,d=Object.keys(this.registry),e=0,f=d;e<f.length;e++){var g=f[e],h=this._run(g,a,b,c);if(h)return h}return null}},{key:"_run",value:function _run(a,b,c,d){if(this.registry[a]){var e=this._schema,f=this.registry[a],g=f.args,h=f.fn,i=h||e.validators[a],j=i.apply(void 0,[b,c].concat(_toConsumableArray(g),[d]));if(!j)return this._error(a,c,g,d)}}},{key:"_register",value:function _register(a,b,c){return this.registry[a]={args:b,fn:c},this}},{key:"_error",value:function _error(a,b,c,d){var e=this._schema,f=this.messages[a]||this.messages["default"]||e.messages[a]||e.messages["default"];return"function"==typeof f&&(f=f.apply(void 0,[d,b].concat(_toConsumableArray(c)))),new _error2["default"](f,d)}}]),a}();exports["default"]=Property,module.exports=exports.default;