Skip to content

Commit

Permalink
feat: add support for typeof, instanceof ({Function\|RegExp}) (#10
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gribnoysup authored and michael-ciniawsky committed Oct 28, 2017
1 parent 96525dd commit 9f01816
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 12 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -28,7 +28,8 @@
"release": "yarn run standard-version"
},
"dependencies": {
"ajv": "^5.0.0"
"ajv": "^5.0.0",
"ajv-keywords": "^2.1.0"
},
"devDependencies": {
"babel-cli": "^6.24.1",
Expand Down
15 changes: 9 additions & 6 deletions src/validateOptions.js
Expand Up @@ -2,15 +2,18 @@ import fs from 'fs';
import path from 'path'; // eslint-disable-line

import Ajv from 'ajv';
import ajvKeywords from 'ajv-keywords';
import ValidationError from './ValidationError';

const validateOptions = (schema, options, name) => {
const ajv = new Ajv({
useDefaults: true,
allErrors: true,
errorDataPath: 'property',
});
const ajv = new Ajv({
useDefaults: true,
allErrors: true,
errorDataPath: 'property',
});

ajvKeywords(ajv, ['instanceof', 'typeof']);

const validateOptions = (schema, options, name) => {
if (typeof schema === 'string') {
schema = fs.readFileSync(path.resolve(schema), 'utf8'); // eslint-disable-line
schema = JSON.parse(schema); // eslint-disable-line
Expand Down
60 changes: 60 additions & 0 deletions test/__snapshots__/index.test.js.snap
@@ -0,0 +1,60 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Error should have errors for every key in options 1`] = `
Array [
Object {
"dataPath": ".string",
"keyword": "type",
"message": "should be string",
"params": Object {
"type": "string",
},
"schemaPath": "#/properties/string/type",
},
Object {
"dataPath": ".array",
"keyword": "type",
"message": "should be array",
"params": Object {
"type": "array",
},
"schemaPath": "#/properties/array/type",
},
Object {
"dataPath": ".object.prop",
"keyword": "type",
"message": "should be boolean",
"params": Object {
"type": "boolean",
},
"schemaPath": "#/properties/object/properties/prop/type",
},
Object {
"dataPath": ".boolean",
"keyword": "type",
"message": "should be boolean",
"params": Object {
"type": "boolean",
},
"schemaPath": "#/properties/boolean/type",
},
Object {
"dataPath": ".type",
"keyword": "typeof",
"message": "should pass \\"typeof\\" keyword validation",
"params": Object {
"keyword": "typeof",
},
"schemaPath": "#/properties/type/typeof",
},
Object {
"dataPath": ".instance",
"keyword": "instanceof",
"message": "should pass \\"instanceof\\" keyword validation",
"params": Object {
"keyword": "instanceof",
},
"schemaPath": "#/properties/instance/instanceof",
},
]
`;
6 changes: 6 additions & 0 deletions test/fixtures/schema.json
Expand Up @@ -20,6 +20,12 @@
},
"boolean": {
"type": "boolean"
},
"type": {
"typeof": "function"
},
"instance": {
"instanceof": "RegExp"
}
},
"additionalProperties": false
Expand Down
30 changes: 25 additions & 5 deletions test/index.test.js
Expand Up @@ -6,21 +6,41 @@ test('Valid', () => {
string: 'hello',
array: [ 'a' ],
object: { prop: false },
boolean: true
boolean: true,
type: function() {},
instance: new RegExp(''),

};

expect(validateOptions('test/fixtures/schema.json', options, 'Loader'))
.toBe(true);
});

test('Error', () => {
describe('Error', () => {
const options = {
string: false,
array: {},
object: { prop: 1 },
boolean: 'hello'
boolean: 'hello',
type: null,
instance: function() {},
};

expect(() => validateOptions('test/fixtures/schema.json', options, '{Name}'))
.toThrowError(/Validation Error\n\n{Name} Invalid Options\n\n/);
const validate = () => validateOptions('test/fixtures/schema.json', options, '{Name}')

test('should throw error', () => {
expect(validate).toThrowError(/Validation Error\n\n{Name} Invalid Options\n\n/);
})

test('should have errors for every key in options', () => {
try {
validate()
} catch(error) {
const expected = ['.string', '.array', '.object.prop', '.boolean', '.type', '.instance'];
const errors = error.err.map(e => e.dataPath)

expect(errors).toMatchObject(expected);
expect(error.err).toMatchSnapshot();
}
})
});
4 changes: 4 additions & 0 deletions yarn.lock
Expand Up @@ -52,6 +52,10 @@ ajv-keywords@^1.0.0:
version "1.5.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"

ajv-keywords@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-2.1.0.tgz#a296e17f7bfae7c1ce4f7e0de53d29cb32162df0"

ajv@^4.7.0, ajv@^4.9.1:
version "4.11.7"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.11.7.tgz#8655a5d86d0824985cc471a1d913fb6729a0ec48"
Expand Down

0 comments on commit 9f01816

Please sign in to comment.