UNPKG

metaforge

Version:

Library that allow you to describe data structures by subset of JavaScript syntax and validate them at runtime

23 lines (20 loc) 941 B
'use strict'; const define = (ctx, name, value) => Object.defineProperty(ctx, name, { enumerable: false, value }); const defaultFormat = dict => `[${dict.path}] => ${dict.cause}`; module.exports = options => { const format = options?.format ?? defaultFormat; return function SchemaError({ path = 'unknown', sample, plan, cause, sampleType }) { [this.count, this.path, this.cause] = [0, path, '']; if (sample) [this.sample, this.sampleType] = [sample, sampleType ?? typeof sample]; if (cause) this.cause = (++this.count, cause); if (plan) this.plan = plan; this.message = format(this); define(this, 'toString', () => this.message); define(this, 'toJSON', () => ({ sample, path, cause: this.cause, count: this.count })); define(this, 'add', cause => { this.cause = this.cause ? `${this.cause}, ${cause}` : cause; this.message = (++this.count, format(this)); return this; }); }; };