mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 23:53:13 +09:00
Port basic lib modules to TypeScript
This commit is contained in:
1 parent
2988cd962c
commit
e0c197b9bc
25 files changed
+1370
-1324
No files matched your search
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* 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.lazySchemaCompilation) {
|
||||
return ajv.compile<T>(schema);
|
||||
}
|
||||
|
||||
let doValidate: ValidateFunction<T>;
|
||||
const validate = <ValidateFunction<T>>function (data) {
|
||||
if (!doValidate) {
|
||||
doValidate = ajv.compile(schema);
|
||||
}
|
||||
let result = doValidate(data);
|
||||
validate.errors = doValidate.errors;
|
||||
return result;
|
||||
}
|
||||
return validate
|
||||
}
|
||||
Reference in new issue
Block a user