UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

53 lines (52 loc) 2.1 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 { inspectObject } from '@backland/utils'; import { CircularDeps } from '../CircularDeps'; import { arrayFieldParse } from './ArrayFieldParse'; import { FieldType } from './FieldType'; export class ArrayField extends FieldType { // static is(item) { return (item === null || item === void 0 ? void 0 : item.typeName) === 'list'; } constructor(def) { super({ def: def, name: 'array' }); _defineProperty(this, "parse", void 0); _defineProperty(this, "utils", { listItemType: {} }); var { parseObjectField } = CircularDeps; try { this.utils.listItemType = parseObjectField("ListItem", def.of, true); } catch (e) { var message = "Filed to parse type:"; message += "\n".concat(inspectObject(def, { tabSize: 2 })); e.stack = message + '\n' + e.stack; throw e; } var self = this; this.parse = this.applyParser({ parse: (input, options) => { if (input === undefined && this.optional) return input; return arrayFieldParse({ arrayOptions: self.def, input, parser: self.utils.listItemType.parse, parserOptions: options }); } }); } } _defineProperty(ArrayField, "create", def => { return new ArrayField(def); }); //# sourceMappingURL=ArrayField.js.map