UNPKG

create-validator-ts

Version:

Create JSON Schema validator from TypeScript.

50 lines (49 loc) 1.86 kB
import path from "node:path"; export const generator = ({ apiFilePath, apiFileCode, schema }) => { var _a; const apiFileName = path.basename(apiFilePath, ".ts"); const isExportedTypeInApiTypes = (apiName) => { return (apiFileCode.includes(`export type ${apiName} =`) || apiFileCode.includes(`export interface ${apiName} {`)); }; const banner = `// @ts-nocheck // eslint-disable // This file is generated by create-validator-ts import Ajv from 'ajv'; import type * as apiTypes from './${apiFileName}'; `; // define SCHEMA to top, and we can refer it as "SCHEMA". // Note: { "$ref": "SCHEMA#/definitions/${apiName}" } const schemaDefinition = `export const SCHEMA = ${JSON.stringify(schema, null, 4)}; const ajv = new Ajv({ removeAdditional: true, useDefaults: true }).addSchema(SCHEMA, "SCHEMA");`; const code = Object.entries((_a = schema.definitions) !== null && _a !== void 0 ? _a : {}) .filter(([apiName]) => { return isExportedTypeInApiTypes(apiName); }) .map(([apiName, _schema]) => { return `export function validate${apiName}(payload: unknown): apiTypes.${apiName} { /** Schema is defined in {@link SCHEMA.definitions.${apiName} } **/ const validator = ajv.getSchema("SCHEMA#/definitions/${apiName}"); const valid = validator(payload); if (!valid) { const error = new Error('Invalid ${apiName}: ' + ajv.errorsText(validator.errors, {dataVar: "${apiName}"})); error.name = "ValidationError"; throw error; } return payload; } export function is${apiName}(payload: unknown): payload is apiTypes.${apiName} { try { validate${apiName}(payload); return true; } catch (error) { return false; } }`; }) .join("\n\n"); return `${banner} ${schemaDefinition} ${code} `; }; //# sourceMappingURL=default-code-generator.js.map