mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 15:24:52 +09:00
Migrate the usage of CommonJS specific code and imports to ESM. Roughly summarised this: - Changes all require() calls to import statement or expressions. - Changes usage of module.exports to export statements. - Replaces require.resolve with import.meta.resolve. - Replaces conditional imports with conditional subpath imports. - Replaces module === require.main with import.meta.main. - Adds the full filename with extention to all relative imports. - Changes all usage of __dirname and __filename to their import.meta equivalents. - Sets "type": "module" in package.json files. - Removes usage of "use strict". - Adds .ts an an alias of .js for the webpack build. Due to differences in how require and import resolves files the plugin entrypoints will have to include the filename and extension after this migration in order to work.
35 lines
893 B
TypeScript
35 lines
893 B
TypeScript
/**
|
|
* JSON schemas used for validating link messages
|
|
* @module lib/schema
|
|
*/
|
|
import { Ajv, JSONSchemaType, ValidateFunction } from "ajv";
|
|
const ajv = new Ajv({
|
|
// These are duplicated in scripts/compile_validator
|
|
allowUnionTypes: true,
|
|
strict: "log",
|
|
strictTuples: false,
|
|
verbose: true,
|
|
});
|
|
|
|
/**
|
|
* Compile JSON schema into validator
|
|
* @param schema - JSON schema to create validator for.
|
|
* @returns Validator for the schema.
|
|
*/
|
|
export function compile<T>(schema: JSONSchemaType<T>) {
|
|
if (typeof global !== "object" || !(global as any).lazySchemaCompilation) {
|
|
return ajv.compile<T>(schema);
|
|
}
|
|
|
|
let doValidate: ValidateFunction<T> | undefined;
|
|
const validate = <ValidateFunction<T>> function (data) {
|
|
if (!doValidate) {
|
|
doValidate = ajv.compile(schema);
|
|
}
|
|
let result = doValidate(data);
|
|
validate.errors = doValidate.errors;
|
|
return result;
|
|
};
|
|
return validate;
|
|
}
|