UNPKG

@auth70/bodyguard

Version:

Fetch API compatible streaming JSON and form data body parser and guard

37 lines (36 loc) 1.09 kB
/** * Helper functions to work with Standard Schema */ /** * Check if a value is a Standard Schema * @param value The value to check * @returns Whether the value is a Standard Schema */ export function isStandardSchema(value) { return typeof value === 'object' && value !== null && '~standard' in value && typeof value['~standard'] === 'object' && value['~standard'].version === 1 && typeof value['~standard'].vendor === 'string' && typeof value['~standard'].validate === 'function'; } /** * Convert StandardSchemaV1 issue to a GenericIssue * @param issue Standard Schema issue * @returns Generic issue format used by Bodyguard */ export function standardIssueToGenericIssue(issue) { // Convert path segments to simple path array const path = issue.path ? issue.path.map(segment => { if (typeof segment === 'object' && 'key' in segment) { return segment.key; } return segment; }) : []; return { code: 'validation_error', path: path, message: issue.message, }; }