UNPKG

@tak-ps/node-cot

Version:

Lightweight JavaScript library for parsing and manipulating TAK messages

36 lines 1.15 kB
import { Value } from '@sinclair/typebox/value'; export default class TypeValidator { static type(type, body, opts) { if (!opts) opts = {}; if (opts.verbose === undefined) opts.verbose = false; if (opts.default === undefined) opts.default = true; if (opts.convert === undefined) opts.convert = true; if (opts.clean === undefined) opts.clean = true; if (opts.default) { Value.Default(type, body); } if (opts.clean) { Value.Clean(type, body); } if (opts.convert) { Value.Convert(type, body); } const result = Value.Check(type, body); if (result) return body; const errors = Value.Errors(type, body); const firstError = errors.First(); if (opts.verbose) { throw new Error(`Internal Validation Error: ${JSON.stringify(firstError)} -- Body: ${JSON.stringify(body)}`); } else { throw new Error(`Internal Validation Error`); } } } //# sourceMappingURL=type.js.map