Skip to content

Commit c2d7b89

Browse files
committed
refactor: convert typeof to inline keyword (to support ajv-pack)
1 parent e880270 commit c2d7b89

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

keywords/typeof.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ module.exports = defFunc;
55
var KNOWN_TYPES = ['undefined', 'string', 'number', 'object', 'function', 'boolean', 'symbol'];
66

77
var definition = defFunc.definition = {
8-
compile: function (schema) {
9-
return typeof schema == 'string' ? singleType : multipleTypes;
10-
11-
function singleType(data) {
12-
return typeof data == schema;
13-
}
14-
15-
function multipleTypes(data) {
16-
return schema.indexOf(typeof data) >= 0;
17-
}
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';
1813
},
1914
metaSchema: {
2015
anyOf: [

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ajv-keywords",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Custom JSON-Schema keywords for ajv validator",
55
"main": "index.js",
66
"scripts": {
@@ -28,7 +28,8 @@
2828
"ajv": ">=4.2.0"
2929
},
3030
"devDependencies": {
31-
"ajv": "^4.6.1",
31+
"ajv": "^4.7.4",
32+
"ajv-pack": "^0.2.0",
3233
"chai": "^3.5.0",
3334
"coveralls": "^2.11.9",
3435
"eslint": "^2.11.1",

spec/typeof.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
'use strict';
22

33
var Ajv = require('ajv');
4+
var ajvPack = require('ajv-pack');
45
var defFunc = require('../keywords/typeof');
56
var defineKeywords = require('..');
67
var should = require('chai').should();
78

89

910
describe('keyword "typeof"', function() {
10-
var ajvs = [ new Ajv, new Ajv, new Ajv ];
11+
var ajvs = [ new Ajv, new Ajv, new Ajv, ajvPack.instance(new Ajv({sourceCode: true})) ];
1112
defFunc(ajvs[0]);
1213
defineKeywords(ajvs[1], 'typeof');
1314
defineKeywords(ajvs[2]);
15+
defFunc(ajvs[3]);
1416

1517
ajvs.forEach(function (ajv, i) {
1618
it('should validate value types #' + i, function() {

0 commit comments

Comments
 (0)