Fix ajv init w/ formats

This commit is contained in:
Rory&
2026-09-05 18:41:53 +02:00
parent 855c274813
commit 1a9139eef8
4 changed files with 23 additions and 18 deletions
Binary file not shown.
Binary file not shown.
+4 -4
View File
@@ -21,13 +21,13 @@ export type StringStringDictionary = {
};
/**
* @TJS-type: number
* @TJS-format: float
* @TJS-type number
* @TJS-format float
*/
export type float = number;
/**
* @TJS-type: string
* @TJS-format: byte
* @TJS-type string
* @TJS-format byte
*/
export type base64str = string;
+19 -14
View File
@@ -16,7 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import Ajv from "ajv";
import Ajv, { SchemaObject } from "ajv";
import addFormats from "ajv-formats";
import fs from "node:fs";
import path from "node:path";
@@ -39,20 +39,25 @@ const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" }).rep
// }
// }
export const ajv = new Ajv({
allErrors: true,
parseDate: true,
allowDate: true,
// schemas: schemas,
coerceTypes: true,
messages: true,
strict: true,
strictRequired: true,
allowUnionTypes: true,
});
export const ajv = (function () {
console.log("[Validation] Initializing Ajv with", Object.entries(schemas).length, "schemas...");
const _ajv = new Ajv({
allErrors: true,
parseDate: true,
allowDate: true,
// schemas: schemas,
coerceTypes: true,
messages: true,
strict: true,
strictRequired: true,
allowUnionTypes: true,
});
addFormats(ajv);
ajv.addSchema(schemas);
addFormats(_ajv);
Object.entries(schemas).forEach((s) => _ajv.addSchema(s[1] as SchemaObject, s[0]));
return _ajv;
})();
export function validateSchema<G extends object>(schema: string, data: G): G {
const valid = ajv.validate(schema, data);