UNPKG

normul

Version:

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

40 lines 1.2 kB
import { isObject } from '../../utils.js'; import { Schema } from '../Schema.js'; export class RecordSchema extends Schema { keySchema; valueSchema; constructor(keySchema, valueSchema) { super(); this.keySchema = keySchema; this.valueSchema = valueSchema; } _normalize(input, ctx) { if (!isObject(input)) { this.makeIssue({ ctx, message: 'Converted to object', level: 'warn', expected: 'object', received: input, }); } const object = { ...input }; const result = {}; for (const key in object) { ctx.path.push('[key]'); const normalizedKey = this.invokeNormalize(this.keySchema, key, ctx); ctx.path.pop(); ctx.path.push(normalizedKey); result[normalizedKey] = this.invokeNormalize(this.valueSchema, object[key], ctx); ctx.path.pop(); } return result; } get partial() { return this; } cloneArgs() { return [this.keySchema, this.valueSchema]; } } //# sourceMappingURL=RecordSchema.js.map