export function generateTSContent({
schema,
compressedSchema,
propertyMappingTable,
enumMappingTables,
compressedInterfaceName,
originalTSInterface,
compressedTSInterface,
}: {
schema: any;
compressedSchema: any;
propertyMappingTable: KeyValue;
enumMappingTables: { [key: string]: KeyValue };
interfaceName: string;
compressedInterfaceName: string;
originalTSInterface: string;
compressedTSInterface: string;
}): string {
return `import Ajv from "https://esm.sh/ajv@8.12.0";
import addFormats from "https://esm.sh/ajv-formats@2.1.1";
${originalTSInterface}
${compressedTSInterface}
export const schemaString = ${JSON.stringify(schema)};
export const schemaCompressedString = ${JSON.stringify(compressedSchema)};
export const propertyMappingTable = ${JSON.stringify(propertyMappingTable)};
export const enumMappingTables = ${JSON.stringify(enumMappingTables)};
const ajv = new Ajv({ allErrors: true });
addFormats(ajv);
const validate = ajv.compile(schemaCompressedString);
export function validateData(data: ${compressedInterfaceName}): { valid: boolean; errors: any } {
const valid = validate(data);
return {
valid,
errors: validate.errors,
};
}
`;
}
export function generateTSContent({
schema,
compressedSchema,
propertyMappingTable,
enumMappingTables,
compressedInterfaceName,
originalTSInterface,
compressedTSInterface,
}: {
schema: any;
compressedSchema: any;
propertyMappingTable: KeyValue;
enumMappingTables: { [key: string]: KeyValue };
interfaceName: string;
compressedInterfaceName: string;
originalTSInterface: string;
compressedTSInterface: string;
}): string {
return `import Ajv from "https://esm.sh/ajv@8.12.0";
import addFormats from "https://esm.sh/ajv-formats@2.1.1";
${originalTSInterface}
${compressedTSInterface}
export const schemaString = ${JSON.stringify(schema)};
export const schemaCompressedString = ${JSON.stringify(compressedSchema)};
export const propertyMappingTable = ${JSON.stringify(propertyMappingTable)};
export const enumMappingTables = ${JSON.stringify(enumMappingTables)};
const ajv = new Ajv({ allErrors: true });
addFormats(ajv);
const validate = ajv.compile(schemaCompressedString);
export function validateData(data: ${compressedInterfaceName}): { valid: boolean; errors: any } {
const valid = validate(data);
return {
valid,
errors: validate.errors,
};
}
`;
}