webpack/schemas/ajv.absolutePath.js

34 lines
812 B
JavaScript
Raw Normal View History

2017-02-02 17:13:15 +08:00
"use strict";
const getErrorFor = (shouldBeAbsolute, data, schema) => {
2018-03-14 22:54:13 +08:00
const message = shouldBeAbsolute
? `The provided value ${JSON.stringify(data)} is not an absolute path!`
: `A relative path is expected. However the provided value ${JSON.stringify(
data
)} is an absolute path!`;
2017-02-02 17:13:15 +08:00
return {
keyword: "absolutePath",
params: { absolutePath: data },
message: message,
2018-03-14 22:54:13 +08:00
parentSchema: schema
2017-02-02 17:13:15 +08:00
};
};
2018-03-14 22:54:13 +08:00
module.exports = ajv =>
ajv.addKeyword("absolutePath", {
errors: true,
type: "string",
compile(expected, schema) {
function callback(data) {
const passes = expected === /^(?!(?:[A-Za-z]:\\|\/).*!)/.test(data);
if (!passes) {
callback.errors.push(getErrorFor(expected, data, schema));
}
return passes;
2017-02-02 17:13:15 +08:00
}
2018-03-14 22:54:13 +08:00
callback.errors = [];
return callback;
2017-02-02 17:13:15 +08:00
}
2018-03-14 22:54:13 +08:00
});