Skip to content

Commit a7c431d

Browse files
committed
refactor: only create definition when keyword is used
1 parent 2022955 commit a7c431d

File tree

5 files changed

+97
-99
lines changed

5 files changed

+97
-99
lines changed

keywords/instanceof.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
module.exports = defFunc;
4-
53
var CONSTRUCTORS = {
64
Object: Object,
75
Array: Array,
@@ -12,44 +10,44 @@ var CONSTRUCTORS = {
1210
RegExp: RegExp
1311
};
1412

15-
/* istanbul ignore else */
16-
if (typeof Buffer != 'undefined')
17-
CONSTRUCTORS.Buffer = Buffer;
13+
module.exports = function defFunc(ajv) {
14+
/* istanbul ignore else */
15+
if (typeof Buffer != 'undefined')
16+
CONSTRUCTORS.Buffer = Buffer;
17+
18+
defFunc.definition = {
19+
compile: function (schema) {
20+
if (typeof schema == 'string') {
21+
var Constructor = getConstructor(schema);
22+
return function (data) {
23+
return data instanceof Constructor;
24+
};
25+
}
1826

19-
var definition = defFunc.definition = {
20-
compile: function (schema) {
21-
if (typeof schema == 'string') {
22-
var Constructor = getConstructor(schema);
27+
var constructors = schema.map(getConstructor);
2328
return function (data) {
24-
return data instanceof Constructor;
29+
for (var i=0; i<constructors.length; i++)
30+
if (data instanceof constructors[i]) return true;
31+
return false;
2532
};
33+
},
34+
CONSTRUCTORS: CONSTRUCTORS,
35+
metaSchema: {
36+
anyOf: [
37+
{ type: 'string' },
38+
{
39+
type: 'array',
40+
items: { type: 'string' }
41+
}
42+
]
2643
}
44+
};
2745

28-
var constructors = schema.map(getConstructor);
29-
return function (data) {
30-
for (var i=0; i<constructors.length; i++)
31-
if (data instanceof constructors[i]) return true;
32-
return false;
33-
};
34-
},
35-
CONSTRUCTORS: CONSTRUCTORS,
36-
metaSchema: {
37-
anyOf: [
38-
{ type: 'string' },
39-
{
40-
type: 'array',
41-
items: { type: 'string' }
42-
}
43-
]
46+
ajv.addKeyword('instanceof', defFunc.definition);
47+
48+
function getConstructor(c) {
49+
var Constructor = CONSTRUCTORS[c];
50+
if (Constructor) return Constructor;
51+
throw new Error('invalid "instanceof" keyword value ' + c);
4452
}
4553
};
46-
47-
function defFunc(ajv) {
48-
ajv.addKeyword('instanceof', definition);
49-
}
50-
51-
function getConstructor(c) {
52-
var Constructor = CONSTRUCTORS[c];
53-
if (Constructor) return Constructor;
54-
throw new Error('invalid "instanceof" keyword value');
55-
}

keywords/propertyNames.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
module.exports = function (ajv) {
4-
ajv.addKeyword('propertyNames', {
3+
module.exports = function defFunc(ajv) {
4+
defFunc.definition = {
55
type: 'object',
66
compile: function(schema) {
77
var validate = ajv.compile(schema);
@@ -44,5 +44,7 @@ module.exports = function (ajv) {
4444
: 'http://json-schema.org/draft-04/schema#'
4545
},
4646
errors: true
47-
});
47+
};
48+
49+
ajv.addKeyword('propertyNames', defFunc.definition);
4850
};

keywords/range.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
'use strict';
22

3-
module.exports = defFunc;
3+
module.exports = function defFunc(ajv) {
4+
defFunc.definition = {
5+
type: 'number',
6+
macro: function (schema, parentSchema) {
7+
var min = schema[0]
8+
, max = schema[1]
9+
, exclusive = parentSchema.exclusiveRange;
410

5-
var definition = defFunc.definition = {
6-
type: 'number',
7-
macro: function (schema, parentSchema) {
8-
var min = schema[0]
9-
, max = schema[1]
10-
, exclusive = parentSchema.exclusiveRange;
11+
validateRangeSchema(min, max, exclusive);
1112

12-
validateRangeSchema(min, max, exclusive);
13+
return {
14+
minimum: min,
15+
exclusiveMinimum: exclusive,
16+
maximum: max,
17+
exclusiveMaximum: exclusive
18+
};
19+
},
20+
metaSchema: {
21+
type: 'array',
22+
minItems: 2,
23+
maxItems: 2,
24+
items: { type: 'number' }
25+
}
26+
};
1327

14-
return {
15-
minimum: min,
16-
exclusiveMinimum: exclusive,
17-
maximum: max,
18-
exclusiveMaximum: exclusive
19-
};
20-
},
21-
metaSchema: {
22-
type: 'array',
23-
minItems: 2,
24-
maxItems: 2,
25-
items: { type: 'number' }
26-
}
27-
};
28-
29-
function defFunc(ajv) {
30-
ajv.addKeyword('range', definition);
28+
ajv.addKeyword('range', defFunc.definition);
3129
ajv.addKeyword('exclusiveRange');
32-
}
3330

34-
function validateRangeSchema(min, max, exclusive) {
35-
if (exclusive !== undefined && typeof exclusive != 'boolean')
36-
throw new Error('Invalid schema for exclusiveRange keyword, should be boolean');
31+
function validateRangeSchema(min, max, exclusive) {
32+
if (exclusive !== undefined && typeof exclusive != 'boolean')
33+
throw new Error('Invalid schema for exclusiveRange keyword, should be boolean');
3734

38-
if (min > max || (exclusive && min == max))
39-
throw new Error('There are no numbers in range');
40-
}
35+
if (min > max || (exclusive && min == max))
36+
throw new Error('There are no numbers in range');
37+
}
38+
};

keywords/regexp.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
module.exports = function (ajv) {
4-
ajv.addKeyword('regexp', {
3+
module.exports = function defFunc(ajv) {
4+
defFunc.definition = {
55
type: 'string',
66
inline: function (it, keyword, schema) {
77
return getRegExp() + '.test(data' + (it.dataLevel || '') + ')';
@@ -29,5 +29,7 @@ module.exports = function (ajv) {
2929
required: ['pattern'],
3030
additionalProperties: false
3131
}
32-
});
32+
};
33+
34+
ajv.addKeyword('regexp', defFunc.definition);
3335
};

keywords/typeof.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
'use strict';
22

3-
module.exports = defFunc;
4-
53
var KNOWN_TYPES = ['undefined', 'string', 'number', 'object', 'function', 'boolean', 'symbol'];
64

7-
var definition = defFunc.definition = {
8-
inline: function (it, keyword, schema) {
9-
var data = 'data' + (it.dataLevel || '');
10-
if (typeof schema == 'string') return 'typeof ' + data + ' == "' + schema + '"';
11-
schema = 'validate.schema' + it.schemaPath + '.' + keyword;
12-
return schema + '.indexOf(typeof ' + data + ') >= 0';
13-
},
14-
metaSchema: {
15-
anyOf: [
16-
{
17-
type: 'string',
18-
enum: KNOWN_TYPES
19-
},
20-
{
21-
type: 'array',
22-
items: {
5+
module.exports = function defFunc(ajv) {
6+
defFunc.definition = {
7+
inline: function (it, keyword, schema) {
8+
var data = 'data' + (it.dataLevel || '');
9+
if (typeof schema == 'string') return 'typeof ' + data + ' == "' + schema + '"';
10+
schema = 'validate.schema' + it.schemaPath + '.' + keyword;
11+
return schema + '.indexOf(typeof ' + data + ') >= 0';
12+
},
13+
metaSchema: {
14+
anyOf: [
15+
{
2316
type: 'string',
2417
enum: KNOWN_TYPES
18+
},
19+
{
20+
type: 'array',
21+
items: {
22+
type: 'string',
23+
enum: KNOWN_TYPES
24+
}
2525
}
26-
}
27-
]
28-
}
29-
};
26+
]
27+
}
28+
};
3029

31-
function defFunc(ajv) {
32-
ajv.addKeyword('typeof', definition);
33-
}
30+
ajv.addKeyword('typeof', defFunc.definition);
31+
};

0 commit comments

Comments
 (0)