UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

43 lines 1.85 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 { ulid } from '@backland/utils'; import { FieldType } from './FieldType'; export var ULID_REGEX = /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/; export class UlidField extends FieldType { constructor() { var def = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; super({ def: def, name: 'ulid' }); _defineProperty(this, "parse", void 0); var { autoCreate } = def; expectedType({ autoCreate }, 'boolean', true); this.parse = this.applyParser({ parse(input) { expectedType({ value: input }, 'string'); if (!ULID_REGEX.test(input)) throw new Error('Invalid ulid.'); return input; }, preParse(input) { if (autoCreate && input === undefined) { return ulid(); } return input; } }); } } _defineProperty(UlidField, "create", def => { return new UlidField(def); }); _defineProperty(UlidField, "isUlid", value => ULID_REGEX.test(value)); //# sourceMappingURL=UlidField.js.map