UNPKG

normul

Version:

Normul is a tiny TypeScript/JavaScript library for data normalization and transformation

44 lines 1.37 kB
import { isArray } from '../../utils.js'; import { Schema } from '../Schema.js'; import { UnionSchema } from './UnionSchema.js'; export class ArraySchema extends Schema { elementSchemas; constructor(elementSchemas) { super(); this.elementSchemas = elementSchemas; } _normalize(input, ctx) { let array; if (isArray(input)) { array = input; } else { this.makeIssue({ ctx, message: 'Converted to array', level: 'warn', expected: 'array', received: input, }); array = (input === null || input === undefined) ? [] : [input]; } let normalizer; if (this.elementSchemas.length === 1) { normalizer = (item, ctx) => this.invokeNormalize(this.elementSchemas[0], item, ctx); } else { const union = new UnionSchema(this.elementSchemas); normalizer = (item, ctx) => this.invokeNormalize(union, item, ctx); } return array.map((item, index) => { ctx.path.push(index); const result = normalizer(item, ctx); ctx.path.pop(); return result; }); } cloneArgs() { return [this.elementSchemas]; } } //# sourceMappingURL=ArraySchema.js.map