Skip to content

Commit 254b603

Browse files
committed
add absolutePath keyword for ajv
1 parent 40517e1 commit 254b603

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎schemas/ajv.absolutePath.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
const os = require("os");
4+
5+
const getErrorFor = (shouldBeAbsolute, data, schema) => {
6+
const message = shouldBeAbsolute ?
7+
`The provided value ${JSON.stringify(data)} is not an absolute path!${os.EOL}`
8+
: `A non absolut path is expected. However the provided value ${JSON.stringify(data)} is an absolute path!${os.EOL}`;
9+
10+
return {
11+
keyword: "absolutePath",
12+
params: { absolutePath: data },
13+
message: message,
14+
parentSchema: schema,
15+
};
16+
};
17+
module.exports = (ajv) => ajv.addKeyword("absolutePath", {
18+
errors: true,
19+
type: "string",
20+
compile(expected, schema) {
21+
function callback(data) {
22+
const passes = expected === /^(?:[a-zA-Z]:)?(?:\/|\\)/.test(data);
23+
if(!passes) {
24+
getErrorFor(expected, data, schema);
25+
callback.errors = [getErrorFor(expected, data, schema)];
26+
}
27+
return passes;
28+
}
29+
callback.errors = [];
30+
return callback;
31+
}
32+
});

0 commit comments

Comments
 (0)