Skip to content

Commit

Permalink
Custom matcher for assets
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasaglia committed Jul 31, 2024
1 parent 8732e2b commit 2262a75
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions docs/static/js/validator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function extraxt_schema_ty(schema)
function extract_schema_ty(schema)
{
if ( !schema )
return;
Expand All @@ -17,7 +17,7 @@ function extraxt_schema_ty(schema)
{
for ( let sub_schema of schema[prop] )
{
let ty = extraxt_schema_ty(sub_schema);
let ty = extract_schema_ty(sub_schema);
if ( ty !== undefined )
return ty;
}
Expand Down Expand Up @@ -252,6 +252,11 @@ class Validator
this._patch_property_schema(pschema, schema_id + "#/$defs/properties/" + pname);
}

this.schema["$defs"].assets["all-assets"] = {
"type": "object",
"asset_oneof": schema_id,
};

prop_map.finalize();

this.validator = new AjvClass({
Expand All @@ -269,6 +274,30 @@ class Validator
keyword: "prop_oneof",
validate: custom_discriminator("a", true),
},
{
keyword: "asset_oneof",
validate: function validate_asset(schema, data, parent_schema, data_cxt)
{
validate_asset.errors = [];

if ( typeof data != "object" || data === null )
return true;

var target_schema;

if ( "layers" in data )
target_schema = this.getSchema(schema + "#/$defs/assets/precomposition");
else
target_schema = this.getSchema(schema + "#/$defs/assets/image");

if ( !target_schema(data, data_cxt) )
{
validate_asset.errors = target_schema.errors;
return false;
}
return true;
},
},
{
keyword: "warn_extra_props",
validate: function warn_extra_props(schema, data, parent_schema, data_cxt)
Expand Down Expand Up @@ -308,7 +337,7 @@ class Validator
let found = {};
for ( let [name, sub_schema] of Object.entries(this.defs[category]) )
{
let ty = extraxt_schema_ty(sub_schema);
let ty = extract_schema_ty(sub_schema);
if ( ty !== undefined )
{
let id = `${id_base}#/$defs/${category}/${name}`;
Expand Down

0 comments on commit 2262a75

Please sign in to comment.