Skip to content

Commit

Permalink
feat(json validation): add catalogue schema
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsJohansen87 committed Feb 27, 2024
1 parent dce505d commit e2d6942
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 9 deletions.
30 changes: 24 additions & 6 deletions packages/lib/src/components/Options.wc.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,40 @@
import { catalogue } from "../stores/catalogue";
import type { Criteria } from "../types/treeData";
import optionsSchema from "../interfaces/options.schema.json";
import catalogueSchema from "../interfaces/catalogue.schema.json";
import { parser } from "@exodus/schemasafe";
import type { LensOptions } from "../types/options";
export let options: object = {};
export let options: LensOptions = {};
export let catalogueData: Criteria[] = [];
/**
* Validate the options against the schema
* Validate the options against the schema before passing them to the store
*/
const parse = parser(optionsSchema, { includeErrors: true });
$: {
const parse = parser(optionsSchema, {
includeErrors: true,
allErrors: true,
});
const validJSON = parse(JSON.stringify(options));
if (options !== {} && options !== "" && validJSON.errors) {
if (validJSON.valid === true) {
$lensOptions = options;
} else if (typeof options === "object") {
console.error("Lens-Options: ", validJSON.errors);
}
}
$: $lensOptions = options;
$: $catalogue = catalogueData;
$: {
const parse = parser(catalogueSchema, {
includeErrors: true,
allErrors: true,
});
const validJSON = parse(JSON.stringify(catalogueData));
if (validJSON.valid === true) {
$catalogue = catalogueData;
} else if (typeof catalogueData === "object") {
console.error("Lens-Options: ", validJSON.errors);
}
}
</script>
130 changes: 130 additions & 0 deletions packages/lib/src/interfaces/catalogue.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Search Parameter Catalogue",
"description": "A catalogue of search parameters",
"type": "array",
"items": {
"$ref": "#/$defs/categoryItem"
},
"$defs": {
"childCategories": {
"type": "array",
"items": {
"$ref": "#/$defs/categoryItem"
}
},
"categoryItem": {
"type": "object",
"properties": {
"key": {
"type": "string",
"pattern": "^.+$"
},
"name": {
"type": "string",
"pattern": "^.+$"
},
"subCategoryName": {
"type": "string",
"pattern": "^.+$"
},
"infoButtonText": {
"type": "array",
"description": "The text to display in the info button",
"items": {
"type": "string",
"pattern": "^.*$"
}
},
"system": {
"type": "string",
"pattern": "^.*$"
},
"fieldType": {
"enum": [
"single-select",
"number",
"autocomplete"
]
},
"type": {
"enum": [
"EQUALS",
"BETWEEN"
]
},
"childCategories": {
"$ref": "#/$defs/childCategories"
},
"criteria": {
"$ref": "#/$defs/criteria"
}
},
"additionalProperties": false,
"unevaluatedProperties": false,
"required": [
"key",
"name"
]
},
"criteria": {
"type": "array",
"items": {
"type": "object",
"properties": {
"key": {
"type": "string",
"pattern": "^.+$"
},
"name": {
"type": "string",
"pattern": "^.+$"
},
"description": {
"type": "string",
"pattern": "^.*$"
},
"infoButtonText": {
"type": "array",
"description": "The text to display in the info button",
"items": {
"type": "string",
"pattern": "^.+$"
}
},
"aggregatedValue": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"value": {
"type": "string",
"pattern": "^.+$"
},
"name": {
"type": "string",
"pattern": "^.+$"
}
},
"additionalProperties": false,
"unevaluatedProperties": false,
"required": [
"value",
"name"
]
}
}
}
},
"additionalProperties": false,
"unevaluatedProperties": false,
"required": [
"key",
"name"
]
}
}
}
}
5 changes: 2 additions & 3 deletions packages/lib/src/interfaces/options.schema.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/product.schema.json",
"title": "Product",
"description": "A product in the catalog",
"title": "Lens Options",
"description": "The options for the lens",
"type": "object",
"properties": {
"iconOptions": {
Expand Down

0 comments on commit e2d6942

Please sign in to comment.