UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

50 lines 2.17 kB
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } import { expectedType } from '@backland/utils'; import { FieldType } from './FieldType'; export class FloatField extends FieldType { constructor() { var def = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; super({ def: def, name: 'float' }); _defineProperty(this, "parse", void 0); _defineProperty(this, "toString", () => "".concat(this.typeName, "(").concat(this.def || '', ")")); var { min, max } = def; expectedType({ max, min }, 'number', true); this.parse = this.applyParser({ parse: input => { expectedType({ value: input }, 'number'); if (max !== undefined && input > max) { throw new Error("".concat(input, " is more than the maximum ").concat(max, ".")); } if (min !== undefined && input < min) { throw new Error("".concat(input, " is less than the minimum ").concat(min, ".")); } return input; }, preParse(input) { if (typeof input === 'string' && input !== '') { var asNumber = +input; if (!isNaN(asNumber)) return asNumber; } return input; } }); } } _defineProperty(FloatField, "create", function () { var def = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return new FloatField(def); }); //# sourceMappingURL=FloatField.js.map