From a4538e4005eba8139d004307bef74392ac172111 Mon Sep 17 00:00:00 2001 From: Mattia Basaglia Date: Mon, 5 Aug 2024 17:35:48 +0200 Subject: [PATCH] Nicer validation error for constants --- docs/static/js/validator.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/static/js/validator.js b/docs/static/js/validator.js index 9729d67..850d39e 100644 --- a/docs/static/js/validator.js +++ b/docs/static/js/validator.js @@ -221,6 +221,15 @@ function custom_discriminator(propname, fail_unknown, default_value=undefined) return validate_fn; } +function patch_schema_enum(schema) +{ + if ( "oneOf" in schema ) + { + schema.enum_oneof = schema.oneOf; + delete schema.oneOf; + } +} + class Validator { constructor(AjvClass, schema_json) @@ -258,6 +267,9 @@ class Validator } this.defs.properties["base-keyframe"].keyframe = true; + for ( let enum_schema of Object.values(this.defs.constants) ) + patch_schema_enum(enum_schema); + this.defs.assets["all-assets"] = { "type": "object", "asset_oneof": schema_id, @@ -355,6 +367,24 @@ class Validator return validate_keyframe.errors.length == 0; } }, + { + keyword: "enum_oneof", + validate: function validate_enum(schema, data, parent_schema, data_cxt) + { + validate_enum.errors = []; + for ( let value of schema ) + if ( value.const === data ) + return true; + + validate_enum.errors.push({ + message: `${data} is not a valid enumeration value`, + type: "error", + instancePath: data_cxt.instancePath, + parentSchema: parent_schema, + }); + return false; + }, + }, { keyword: "warn_extra_props", validate: function warn_extra_props(schema, data, parent_schema, data_cxt)