UNPKG

@aws-lambda-powertools/parser

Version:
41 lines (40 loc) 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = parse; const errors_js_1 = require("./errors.js"); // Implementation function parse(data, envelope, schema, safeParse) { if (envelope && safeParse) { // biome-ignore lint/suspicious/noExplicitAny: at least for now, we need to broaden the type because the envelope's parse and safeParse methods are not typed with StandardSchemaV1 but with ZodSchema return envelope.safeParse(data, schema); } if (envelope) { // biome-ignore lint/suspicious/noExplicitAny: at least for now, we need to broaden the type because the envelope's parse and safeParse methods are not typed with StandardSchemaV1 but with ZodSchema return envelope.parse(data, schema); } const result = schema['~standard'].validate(data); /* v8 ignore next -- @preserve */ if (result instanceof Promise) { throw new errors_js_1.ParseError('Schema parsing supports only synchronous validation'); } if (result.issues) { const error = new errors_js_1.ParseError('Failed to parse schema', { cause: result.issues, }); if (safeParse) { return { success: false, error, originalEvent: data, }; } throw error; } if (safeParse) { return { success: true, data: result.value, }; } return result.value; }