Commit 4baac36e authored by David Sveningsson's avatar David Sveningsson
Browse files

feat(htmlvalidate): add `getElementsSchema()` to get effective elements schema

fixes #78
parent 1dd81d99
Pipeline #234686144 passed with stages
in 10 minutes and 18 seconds
......@@ -535,13 +535,27 @@ describe("HtmlValidate", () => {
`);
});
it("getConfiurationSchema() should get configuration schema", () => {
it("getConfiurationSchema() should get effective configuration schema", () => {
expect.assertions(1);
const htmlvalidate = new HtmlValidate();
const schema = htmlvalidate.getConfigurationSchema();
expect(schema).not.toBeUndefined();
});
it("getElementsSchema() should get effective elements schema", () => {
expect.assertions(1);
const htmlvalidate = new HtmlValidate();
const schema = htmlvalidate.getElementsSchema("./path/to/elements.json");
expect(schema).not.toBeUndefined();
});
it("getElementsSchema() should not require filename", () => {
expect.assertions(1);
const htmlvalidate = new HtmlValidate();
const schema = htmlvalidate.getElementsSchema();
expect(schema).not.toBeUndefined();
});
it("getRuleDocumentation() should delegate call to engine", () => {
expect.assertions(1);
const htmlvalidate = new HtmlValidate();
......
......@@ -221,10 +221,27 @@ class HtmlValidate {
}, [] as string[]);
}
/**
* Get effective configuration schema.
*/
public getConfigurationSchema(): SchemaObject {
return configurationSchema;
}
/**
* Get effective metadata element schema.
*
* If a filename is given the configured plugins can extend the
* schema. Filename must not be an existing file or a filetype normally
* handled by html-validate but the path will be used when resolving
* configuration. As a rule-of-thumb, set it to the elements json file.
*/
public getElementsSchema(filename?: string): SchemaObject {
const config = this.getConfigFor(filename ?? "inline");
const metaTable = config.getMetaTable();
return metaTable.getJSONSchema();
}
/**
* Get contextual documentation for the given rule.
*
......
import Ajv, { KeywordDefinition, ValidateFunction } from "ajv";
import { DataValidateFunction, DataValidationCxt } from "ajv/dist/types";
import { DataValidateFunction, DataValidationCxt, SchemaObject } from "ajv/dist/types";
import deepmerge from "deepmerge";
import jsonMergePatch from "json-merge-patch";
import { HtmlElement } from "../dom";
......@@ -76,9 +76,7 @@ const ajvRegexpKeyword: KeywordDefinition = {
export class MetaTable {
public readonly elements: ElementTable;
/* jsonMergePatch.apply returns Object */
/* eslint-disable-next-line @typescript-eslint/ban-types */
private schema: Object;
private schema: SchemaObject;
public constructor() {
this.elements = {};
......@@ -218,6 +216,10 @@ export class MetaTable {
return ajv.compile<MetaDataTable>(this.schema);
}
public getJSONSchema(): SchemaObject {
return this.schema;
}
/**
* Finds the global element definition and merges each known element with the
* global, e.g. to assign global attributes.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment