Skip to content

Commit

Permalink
Nicer validation error for constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasaglia committed Aug 6, 2024
1 parent ffa3bf6 commit 826e834
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/static/js/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 826e834

Please sign in to comment.