scripts/create-validators.js
/**
 * Create a standalone JS module including pre-compiled validation functions for
 * each of the specified schemas that can be used without additional runtime
 * dependencies.
 *
 * @remarks Schemas referred to by `$ref` must be specified separately.
 *
 * @remarks The generated validation functions are named as follows:
 * `validate<Capitalized schema title keyword>`.
 *
 * @param schemaFiles an array of paths to JSON schemas for which to create
 * validation functions
 * @param refSchemaFiles an array of paths to JSON schemas resolving $ref
 * references
 * @param moduleFile the CommonJS module file containing the generated
 * validation functions
 * @param moduleFormat generated JS module format, either "esm" or "commonjs"
 * (optional, default is "commonjs")
 */
Create a standalone JS module including pre-compiled validation functions for
each of the specified schemas that can be used without additional runtime
dependencies.

@remarks Schemas referred to by `$ref` must be specified separately.

@remarks The generated validation functions are named as follows:
`validate`.


Create a standalone JS module including pre - compiled validation functions for each of the specified schemas that can be used without additional runtime dependencies . @remarks Schemas referred to by ` $ref ` must be specified separately . @remarks The generated validation functions are named as follows : ` validate ` .
function createValidators(schemaFiles, refSchemaFiles, moduleFile, moduleFormat) {
    const refSchemas = [];
    const modules = [];
    for (const refFile of refSchemaFiles) {
        refSchemas.push(JSON.parse(fse.readFileSync(refFile)));
    }
    for (const file of schemaFiles) {
        const schema = JSON.parse(fse.readFileSync(file));
        const validate = validator(schema, { includeErrors: true, schemas: refSchemas, allowUnusedKeywords: true });
        const moduleFunc = `validate${schema.title[0].toUpperCase()}${schema.title.substring(1)}`;
        modules.push([moduleFunc, `const ${moduleFunc} = ${validate.toModule()}\n`]);
    }
    let code = String.raw
        `// ----------------------------------------------------------------------------"
// This file was generated by a tool, do not modify it directly.
// ----------------------------------------------------------------------------
`;
    code += modules.map(m => m[1]).join("\n");
    switch (moduleFormat) {
        case "esm":
            code += `export { ${modules.map(m => m[0]).join(", ")} };`;
            break;
        case "commonjs":
        default:
            code += `module.exports = { ${modules.map(m => m[0]).join(", ")} };`;
            break;
    }
    fse.writeFileSync(moduleFile, code);
}
function createValidators ( schemaFiles , refSchemaFiles , moduleFile , moduleFormat ) { const refSchemas = [ ] ; const modules = [ ] ; for ( const refFile of refSchemaFiles ) { refSchemas . push ( JSON . parse ( fse . readFileSync ( refFile ) ) ) ; } for ( const file of schemaFiles ) { const schema = JSON . parse ( fse . readFileSync ( file ) ) ; const validate = validator ( schema , { includeErrors : true , schemas : refSchemas , allowUnusedKeywords : true } ) ; const moduleFunc = ` ${ schema . title [ 0 ] . toUpperCase ( ) } ${ schema . title . substring ( 1 ) } ` ; modules . push ( [ moduleFunc , ` ${ moduleFunc } ${ validate . toModule ( ) } \n ` ] ) ; } let code = String . raw ` ` ; code += modules . map ( m => m [ 1 ] ) . join ( "\n" ) ; switch ( moduleFormat ) { case "esm" : code += ` ${ modules . map ( m => m [ 0 ] ) . join ( ", " ) } ` ; break ; case "commonjs" : default : code += ` ${ modules . map ( m => m [ 0 ] ) . join ( ", " ) } ` ; break ; } fse . writeFileSync ( moduleFile , code ) ; }
Create a standalone JS module including pre-compiled validation functions for
 each of the specified schemas that can be used without additional runtime
 dependencies.
Create a standalone JS module including pre - compiled validation functions for each of the specified schemas that can be used without additional runtime dependencies .