import {createGenerator} from 'ts-json-schema-generator'; import {getValidatableTypes} from './get-validatable-types.js'; import {JSONSchema7} from 'json-schema'; /** * Compile the JSON schema for a path */ export function compileSchema(path: string, tsconfig: string): [schma: JSONSchema7, type: string] { const generator = createGenerator({ path, tsconfig, extraTags: ['elasticsearch'], skipTypeCheck: true, }); // @ts-expect-error private access const program = generator.program; const schemaNames = getValidatableTypes(program); const fullSchema = { $schema: 'http://json-schema.org/draft-07/schema#', definitions: {}, }; for (const schema of schemaNames) { Object.assign(fullSchema.definitions, generator.createSchema(schema).definitions); } const schemaTypes = `import {JSONSchema7} from 'json-schema';\n\nexport interface SchemaMap {\n${[ ...schemaNames, ] .map(schemaName => ` '${schemaName}': core.${schemaName};`) .join('\n')}\n}\n\nconst schema: JSONSchema7;\nexport default schema;`; return [fullSchema, schemaTypes]; }