Skip to content

Commit

Permalink
fix: better handle absolute paths (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jan 14, 2020
1 parent 6caaa59 commit 1fa2930
Show file tree
Hide file tree
Showing 6 changed files with 1,289 additions and 2,379 deletions.
3,525 changes: 1,169 additions & 2,356 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions package.json
Expand Up @@ -47,30 +47,30 @@
"ajv-keywords": "^3.4.1"
},
"devDependencies": {
"@babel/cli": "^7.7.4",
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@types/json-schema": "^7.0.3",
"@webpack-contrib/defaults": "^6.2.0",
"@babel/cli": "^7.8.3",
"@babel/core": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"@commitlint/cli": "^8.3.4",
"@commitlint/config-conventional": "^8.3.4",
"@types/json-schema": "^7.0.4",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^24.9.0",
"commitlint-azure-pipelines-cli": "^1.0.2",
"commitlint-azure-pipelines-cli": "^1.0.3",
"cross-env": "^6.0.3",
"del": "^5.1.0",
"del-cli": "^3.0.0",
"eslint": "^6.7.1",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-import": "^2.18.2",
"husky": "^3.1.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-import": "^2.20.0",
"husky": "^4.0.6",
"jest": "^24.9.0",
"jest-junit": "^9.0.0",
"jest-junit": "^10.0.0",
"lint-staged": "^9.5.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
"standard-version": "^7.0.1",
"typescript": "^3.7.2"
"typescript": "^3.7.4"
},
"keywords": [
"webpack"
Expand Down
10 changes: 7 additions & 3 deletions src/keywords/absolutePath.js
Expand Up @@ -50,8 +50,6 @@ function addAbsolutePathKeyword(ajv) {
function callback(data) {
let passes = true;
const isExclamationMarkPresent = data.includes('!');
const isCorrectAbsoluteOrRelativePath =
schema === /^(?:[A-Za-z]:\\|\/)/.test(data);

if (isExclamationMarkPresent) {
callback.errors = [
Expand All @@ -66,7 +64,13 @@ function addAbsolutePathKeyword(ajv) {
passes = false;
}

if (!isCorrectAbsoluteOrRelativePath) {
// ?:[A-Za-z]:\\ - Windows absolute path
// \\\\ - Windows network absolute path
// \/ - Unix-like OS absolute path
const isCorrectAbsolutePath =
schema === /^(?:[A-Za-z]:(\\|\/)|\\\\|\/)/.test(data);

if (!isCorrectAbsolutePath) {
callback.errors = [getErrorFor(schema, parentSchema, data)];
passes = false;
}
Expand Down
37 changes: 31 additions & 6 deletions test/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions test/fixtures/schema.json
Expand Up @@ -3715,6 +3715,10 @@
},
"enumNested": {
"enum": [1, 2, 3, [4, 5, 6]]
},
"testAbsolutePath": {
"type": "string",
"absolutePath": true
}
}
}
64 changes: 64 additions & 0 deletions test/index.test.js
Expand Up @@ -276,6 +276,30 @@ describe('Validation', () => {
noTypeLikeArrayAdditionalItems: true,
});

createSuccessTestCase('absolutePath', {
testAbsolutePath: '/directory/deep/tree',
});

createSuccessTestCase('absolutePath #1', {
testAbsolutePath: 'c:\\directory\\deep\\tree',
});

createSuccessTestCase('absolutePath #2', {
testAbsolutePath: 'C:\\directory\\deep\\tree',
});

createSuccessTestCase('absolutePath #3', {
testAbsolutePath: 'C:/directory/deep/tree',
});

createSuccessTestCase('absolutePath #4', {
testAbsolutePath: '\\\\server\\directory\\deep\\tree',
});

createSuccessTestCase('absolutePath #5', {
testAbsolutePath: '//server/directory/deep/tree',
});

createSuccessTestCase('$data', {
dollarData: {
smaller: 5,
Expand Down Expand Up @@ -2813,4 +2837,44 @@ describe('Validation', () => {
},
(msg) => expect(msg).toMatchSnapshot()
);

createFailedTestCase(
'absolute path',
{
testAbsolutePath: 'bar',
},
(msg) => expect(msg).toMatchSnapshot()
);

createFailedTestCase(
'absolute path #1',
{
testAbsolutePath: 'bar\\\\baz',
},
(msg) => expect(msg).toMatchSnapshot()
);

createFailedTestCase(
'absolute path #2',
{
testAbsolutePath: 'bar/baz',
},
(msg) => expect(msg).toMatchSnapshot()
);

createFailedTestCase(
'absolute path #3',
{
testAbsolutePath: '.',
},
(msg) => expect(msg).toMatchSnapshot()
);

createFailedTestCase(
'absolute path #4',
{
testAbsolutePath: '..',
},
(msg) => expect(msg).toMatchSnapshot()
);
});

0 comments on commit 1fa2930

Please sign in to comment.