Commit 444a4726 authored by David Sveningsson's avatar David Sveningsson

fix(meta): allow regexp literal in element schema

fixes #70
parent 3286b986
Pipeline #128420834 passed with stages
in 9 minutes and 5 seconds
......@@ -19,6 +19,9 @@ jest.mock("ajv", () => {
public addMetaSchema(): void {
/* do nothing */
}
public addKeyword(): void {
/* do nothing */
}
}
return MockAjv;
});
......
......@@ -175,3 +175,28 @@ it("should report parser-error when last tag is left unopened", () => {
}
`);
});
it("should allow inline metadata", () => {
expect.assertions(1);
const htmlvalidate = new HtmlValidate({
elements: [
{
foo: {
attributes: {
"string-value": ["foo"],
"string-regexp": ["/foo/"],
"literal-regexp": [/foo/],
},
},
},
],
});
const report = htmlvalidate.validateString(
[
'<foo string="foo"></foo>',
'<foo string-regexp="foo"></foo>',
'<foo literal-regexp="foo"></foo>',
].join("\n")
);
expect(report).toBeValid();
});
......@@ -16,6 +16,9 @@ jest.mock("ajv", () => {
public addMetaSchema(): void {
/* do nothing */
}
public addKeyword(): void {
/* do nothing */
}
}
return MockAjv;
});
......
......@@ -37,6 +37,37 @@ function clone(src: any): any {
return JSON.parse(JSON.stringify(src));
}
/**
* AJV keyword "regexp" to validate the type to be a regular expression.
* Injects errors with the "type" keyword to give the same output.
*/
/* istanbul ignore next: manual testing */
const ajvRegexpValidate: Ajv.ValidateFunction = function(
data: any,
dataPath: string
): boolean {
const valid = data instanceof RegExp;
if (!valid) {
ajvRegexpValidate.errors = [
{
dataPath,
schemaPath: undefined,
keyword: "type",
message: "should be regexp",
params: {
keyword: "type",
},
},
];
}
return valid;
};
const ajvRegexpKeyword: Ajv.KeywordDefinition = {
schema: false,
errors: true,
validate: ajvRegexpValidate,
};
export class MetaTable {
public readonly elements: ElementTable;
private schema: object;
......@@ -79,6 +110,7 @@ export class MetaTable {
): void {
const ajv = new Ajv({ jsonPointers: true });
ajv.addMetaSchema(require("ajv/lib/refs/json-schema-draft-06.json"));
ajv.addKeyword("regexp", ajvRegexpKeyword);
const validator = ajv.compile(this.schema);
const valid = validator(obj);
if (!valid) {
......
......@@ -222,7 +222,9 @@
"^.*$": {
"type": "array",
"uniqueItems": true,
"items": { "type": "string" }
"items": {
"anyOf": [{ "type": "string" }, { "regexp": true }]
}
}
}
},
......
Markdown is supported
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