ajv.spec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. 'use strict';
  2. var Ajv = require('./ajv')
  3. , should = require('./chai').should()
  4. , stableStringify = require('fast-json-stable-stringify');
  5. describe('Ajv', function () {
  6. var ajv;
  7. beforeEach(function() {
  8. ajv = new Ajv;
  9. });
  10. it('should create instance', function() {
  11. ajv .should.be.instanceof(Ajv);
  12. });
  13. describe('compile method', function() {
  14. it('should compile schema and return validating function', function() {
  15. var validate = ajv.compile({ type: 'integer' });
  16. validate .should.be.a('function');
  17. validate(1) .should.equal(true);
  18. validate(1.1) .should.equal(false);
  19. validate('1') .should.equal(false);
  20. });
  21. it('should cache compiled functions for the same schema', function() {
  22. var v1 = ajv.compile({ $id: '//e.com/int.json', type: 'integer', minimum: 1 });
  23. var v2 = ajv.compile({ $id: '//e.com/int.json', minimum: 1, type: 'integer' });
  24. v1 .should.equal(v2);
  25. });
  26. it('should throw if different schema has the same id', function() {
  27. ajv.compile({ $id: '//e.com/int.json', type: 'integer' });
  28. should.throw(function() {
  29. ajv.compile({ $id: '//e.com/int.json', type: 'integer', minimum: 1 });
  30. });
  31. });
  32. it('should throw if invalid schema is compiled', function() {
  33. should.throw(function() {
  34. ajv.compile({ type: null });
  35. });
  36. });
  37. it('should throw if compiled schema has an invalid JavaScript code', function() {
  38. ajv.addKeyword('even', { inline: badEvenCode });
  39. var schema = { even: true };
  40. var validate = ajv.compile(schema);
  41. validate(2) .should.equal(true);
  42. validate(3) .should.equal(false);
  43. schema = { even: false };
  44. should.throw(function() {
  45. ajv.compile(schema);
  46. });
  47. function badEvenCode(it, keyword, _schema) {
  48. var op = _schema ? '===' : '!==='; // invalid on purpose
  49. return 'data' + (it.dataLevel || '') + ' % 2 ' + op + ' 0';
  50. }
  51. });
  52. });
  53. describe('validate method', function() {
  54. it('should compile schema and validate data against it', function() {
  55. ajv.validate({ type: 'integer' }, 1) .should.equal(true);
  56. ajv.validate({ type: 'integer' }, '1') .should.equal(false);
  57. ajv.validate({ type: 'string' }, 'a') .should.equal(true);
  58. ajv.validate({ type: 'string' }, 1) .should.equal(false);
  59. });
  60. it('should validate against previously compiled schema by id (also see addSchema)', function() {
  61. ajv.validate({ $id: '//e.com/int.json', type: 'integer' }, 1) .should.equal(true);
  62. ajv.validate('//e.com/int.json', 1) .should.equal(true);
  63. ajv.validate('//e.com/int.json', '1') .should.equal(false);
  64. ajv.compile({ $id: '//e.com/str.json', type: 'string' }) .should.be.a('function');
  65. ajv.validate('//e.com/str.json', 'a') .should.equal(true);
  66. ajv.validate('//e.com/str.json', 1) .should.equal(false);
  67. });
  68. it('should throw exception if no schema with ref', function() {
  69. ajv.validate({ $id: 'integer', type: 'integer' }, 1) .should.equal(true);
  70. ajv.validate('integer', 1) .should.equal(true);
  71. should.throw(function() { ajv.validate('string', 'foo'); });
  72. });
  73. it('should validate schema fragment by ref', function() {
  74. ajv.addSchema({
  75. "$id": "http://e.com/types.json",
  76. "definitions": {
  77. "int": { "type": "integer" },
  78. "str": { "type": "string" }
  79. }
  80. });
  81. ajv.validate('http://e.com/types.json#/definitions/int', 1) .should.equal(true);
  82. ajv.validate('http://e.com/types.json#/definitions/int', '1') .should.equal(false);
  83. });
  84. it('should return schema fragment by id', function() {
  85. ajv.addSchema({
  86. "$id": "http://e.com/types.json",
  87. "definitions": {
  88. "int": { "$id": "#int", "type": "integer" },
  89. "str": { "$id": "#str", "type": "string" }
  90. }
  91. });
  92. ajv.validate('http://e.com/types.json#int', 1) .should.equal(true);
  93. ajv.validate('http://e.com/types.json#int', '1') .should.equal(false);
  94. });
  95. });
  96. describe('addSchema method', function() {
  97. it('should add and compile schema with key', function() {
  98. ajv.addSchema({ type: 'integer' }, 'int');
  99. var validate = ajv.getSchema('int');
  100. validate .should.be.a('function');
  101. validate(1) .should.equal(true);
  102. validate(1.1) .should.equal(false);
  103. validate('1') .should.equal(false);
  104. ajv.validate('int', 1) .should.equal(true);
  105. ajv.validate('int', '1') .should.equal(false);
  106. });
  107. it('should add and compile schema without key', function() {
  108. ajv.addSchema({ type: 'integer' });
  109. ajv.validate('', 1) .should.equal(true);
  110. ajv.validate('', '1') .should.equal(false);
  111. });
  112. it('should add and compile schema with id', function() {
  113. ajv.addSchema({ $id: '//e.com/int.json', type: 'integer' });
  114. ajv.validate('//e.com/int.json', 1) .should.equal(true);
  115. ajv.validate('//e.com/int.json', '1') .should.equal(false);
  116. });
  117. it('should normalize schema keys and ids', function() {
  118. ajv.addSchema({ $id: '//e.com/int.json#', type: 'integer' }, 'int#');
  119. ajv.validate('int', 1) .should.equal(true);
  120. ajv.validate('int', '1') .should.equal(false);
  121. ajv.validate('//e.com/int.json', 1) .should.equal(true);
  122. ajv.validate('//e.com/int.json', '1') .should.equal(false);
  123. ajv.validate('int#/', 1) .should.equal(true);
  124. ajv.validate('int#/', '1') .should.equal(false);
  125. ajv.validate('//e.com/int.json#/', 1) .should.equal(true);
  126. ajv.validate('//e.com/int.json#/', '1') .should.equal(false);
  127. });
  128. it('should add and compile array of schemas with ids', function() {
  129. ajv.addSchema([
  130. { $id: '//e.com/int.json', type: 'integer' },
  131. { $id: '//e.com/str.json', type: 'string' }
  132. ]);
  133. var validate0 = ajv.getSchema('//e.com/int.json');
  134. var validate1 = ajv.getSchema('//e.com/str.json');
  135. validate0(1) .should.equal(true);
  136. validate0('1') .should.equal(false);
  137. validate1('a') .should.equal(true);
  138. validate1(1) .should.equal(false);
  139. ajv.validate('//e.com/int.json', 1) .should.equal(true);
  140. ajv.validate('//e.com/int.json', '1') .should.equal(false);
  141. ajv.validate('//e.com/str.json', 'a') .should.equal(true);
  142. ajv.validate('//e.com/str.json', 1) .should.equal(false);
  143. });
  144. it('should throw on duplicate key', function() {
  145. ajv.addSchema({ type: 'integer' }, 'int');
  146. should.throw(function() {
  147. ajv.addSchema({ type: 'integer', minimum: 1 }, 'int');
  148. });
  149. });
  150. it('should throw on duplicate normalized key', function() {
  151. ajv.addSchema({ type: 'number' }, 'num');
  152. should.throw(function() {
  153. ajv.addSchema({ type: 'integer' }, 'num#');
  154. });
  155. should.throw(function() {
  156. ajv.addSchema({ type: 'integer' }, 'num#/');
  157. });
  158. });
  159. it('should allow only one schema without key and id', function() {
  160. ajv.addSchema({ type: 'number' });
  161. should.throw(function() {
  162. ajv.addSchema({ type: 'integer' });
  163. });
  164. should.throw(function() {
  165. ajv.addSchema({ type: 'integer' }, '');
  166. });
  167. should.throw(function() {
  168. ajv.addSchema({ type: 'integer' }, '#');
  169. });
  170. });
  171. it('should throw if schema is not an object', function() {
  172. should.throw(function() { ajv.addSchema('foo'); });
  173. });
  174. it('should throw if schema id is not a string', function() {
  175. try {
  176. ajv.addSchema({ $id: 1, type: 'integer' });
  177. throw new Error('should have throw exception');
  178. } catch(e) {
  179. e.message .should.equal('schema id must be string');
  180. }
  181. });
  182. it('should return instance of itself', function() {
  183. var res = ajv.addSchema({ type: 'integer' }, 'int');
  184. res.should.equal(ajv);
  185. });
  186. });
  187. describe('getSchema method', function() {
  188. it('should return compiled schema by key', function() {
  189. ajv.addSchema({ type: 'integer' }, 'int');
  190. var validate = ajv.getSchema('int');
  191. validate(1) .should.equal(true);
  192. validate('1') .should.equal(false);
  193. });
  194. it('should return compiled schema by id or ref', function() {
  195. ajv.addSchema({ $id: '//e.com/int.json', type: 'integer' });
  196. var validate = ajv.getSchema('//e.com/int.json');
  197. validate(1) .should.equal(true);
  198. validate('1') .should.equal(false);
  199. });
  200. it('should return compiled schema without key or with empty key', function() {
  201. ajv.addSchema({ type: 'integer' });
  202. var validate = ajv.getSchema('');
  203. validate(1) .should.equal(true);
  204. validate('1') .should.equal(false);
  205. var v = ajv.getSchema();
  206. v(1) .should.equal(true);
  207. v('1') .should.equal(false);
  208. });
  209. it('should return schema fragment by ref', function() {
  210. ajv.addSchema({
  211. "$id": "http://e.com/types.json",
  212. "definitions": {
  213. "int": { "type": "integer" },
  214. "str": { "type": "string" }
  215. }
  216. });
  217. var vInt = ajv.getSchema('http://e.com/types.json#/definitions/int');
  218. vInt(1) .should.equal(true);
  219. vInt('1') .should.equal(false);
  220. });
  221. it('should return schema fragment by ref with protocol-relative URIs', function() {
  222. ajv.addSchema({
  223. "$id": "//e.com/types.json",
  224. "definitions": {
  225. "int": { "type": "integer" },
  226. "str": { "type": "string" }
  227. }
  228. });
  229. var vInt = ajv.getSchema('//e.com/types.json#/definitions/int');
  230. vInt(1) .should.equal(true);
  231. vInt('1') .should.equal(false);
  232. });
  233. it('should return schema fragment by id', function() {
  234. ajv.addSchema({
  235. "$id": "http://e.com/types.json",
  236. "definitions": {
  237. "int": { "$id": "#int", "type": "integer" },
  238. "str": { "$id": "#str", "type": "string" }
  239. }
  240. });
  241. var vInt = ajv.getSchema('http://e.com/types.json#int');
  242. vInt(1) .should.equal(true);
  243. vInt('1') .should.equal(false);
  244. });
  245. });
  246. describe('removeSchema method', function() {
  247. it('should remove schema by key', function() {
  248. var schema = { type: 'integer' }
  249. , str = stableStringify(schema);
  250. ajv.addSchema(schema, 'int');
  251. var v = ajv.getSchema('int');
  252. v .should.be.a('function');
  253. ajv._cache.get(str).validate .should.equal(v);
  254. ajv.removeSchema('int');
  255. should.not.exist(ajv.getSchema('int'));
  256. should.not.exist(ajv._cache.get(str));
  257. });
  258. it('should remove schema by id', function() {
  259. var schema = { $id: '//e.com/int.json', type: 'integer' }
  260. , str = stableStringify(schema);
  261. ajv.addSchema(schema);
  262. var v = ajv.getSchema('//e.com/int.json');
  263. v .should.be.a('function');
  264. ajv._cache.get(str).validate .should.equal(v);
  265. ajv.removeSchema('//e.com/int.json');
  266. should.not.exist(ajv.getSchema('//e.com/int.json'));
  267. should.not.exist(ajv._cache.get(str));
  268. });
  269. it('should remove schema by schema object', function() {
  270. var schema = { type: 'integer' }
  271. , str = stableStringify(schema);
  272. ajv.addSchema(schema);
  273. ajv._cache.get(str) .should.be.an('object');
  274. ajv.removeSchema({ type: 'integer' });
  275. should.not.exist(ajv._cache.get(str));
  276. });
  277. it('should remove schema with id by schema object', function() {
  278. var schema = { $id: '//e.com/int.json', type: 'integer' }
  279. , str = stableStringify(schema);
  280. ajv.addSchema(schema);
  281. ajv._cache.get(str) .should.be.an('object');
  282. ajv.removeSchema({ $id: '//e.com/int.json', type: 'integer' });
  283. // should.not.exist(ajv.getSchema('//e.com/int.json'));
  284. should.not.exist(ajv._cache.get(str));
  285. });
  286. it('should not throw if there is no schema with passed id', function() {
  287. should.not.exist(ajv.getSchema('//e.com/int.json'));
  288. should.not.throw(function() {
  289. ajv.removeSchema('//e.com/int.json');
  290. });
  291. });
  292. it('should remove all schemas but meta-schemas if called without an arguments', function() {
  293. var schema1 = { $id: '//e.com/int.json', type: 'integer' }
  294. , str1 = stableStringify(schema1);
  295. ajv.addSchema(schema1);
  296. ajv._cache.get(str1) .should.be.an('object');
  297. var schema2 = { type: 'integer' }
  298. , str2 = stableStringify(schema2);
  299. ajv.addSchema(schema2);
  300. ajv._cache.get(str2) .should.be.an('object');
  301. ajv.removeSchema();
  302. should.not.exist(ajv._cache.get(str1));
  303. should.not.exist(ajv._cache.get(str2));
  304. });
  305. it('should remove all schemas but meta-schemas with key/id matching pattern', function() {
  306. var schema1 = { $id: '//e.com/int.json', type: 'integer' }
  307. , str1 = stableStringify(schema1);
  308. ajv.addSchema(schema1);
  309. ajv._cache.get(str1) .should.be.an('object');
  310. var schema2 = { $id: 'str.json', type: 'string' }
  311. , str2 = stableStringify(schema2);
  312. ajv.addSchema(schema2, '//e.com/str.json');
  313. ajv._cache.get(str2) .should.be.an('object');
  314. var schema3 = { type: 'integer' }
  315. , str3 = stableStringify(schema3);
  316. ajv.addSchema(schema3);
  317. ajv._cache.get(str3) .should.be.an('object');
  318. ajv.removeSchema(/e\.com/);
  319. should.not.exist(ajv._cache.get(str1));
  320. should.not.exist(ajv._cache.get(str2));
  321. ajv._cache.get(str3) .should.be.an('object');
  322. });
  323. it('should return instance of itself', function() {
  324. var res = ajv
  325. .addSchema({ type: 'integer' }, 'int')
  326. .removeSchema('int');
  327. res.should.equal(ajv);
  328. });
  329. });
  330. describe('addFormat method', function() {
  331. it('should add format as regular expression', function() {
  332. ajv.addFormat('identifier', /^[a-z_$][a-z0-9_$]*$/i);
  333. testFormat();
  334. });
  335. it('should add format as string', function() {
  336. ajv.addFormat('identifier', '^[A-Za-z_$][A-Za-z0-9_$]*$');
  337. testFormat();
  338. });
  339. it('should add format as function', function() {
  340. ajv.addFormat('identifier', function (str) { return /^[a-z_$][a-z0-9_$]*$/i.test(str); });
  341. testFormat();
  342. });
  343. it('should add format as object', function() {
  344. ajv.addFormat('identifier', {
  345. validate: function (str) { return /^[a-z_$][a-z0-9_$]*$/i.test(str); },
  346. });
  347. testFormat();
  348. });
  349. it('should return instance of itself', function() {
  350. var res = ajv.addFormat('identifier', /^[a-z_$][a-z0-9_$]*$/i);
  351. res.should.equal(ajv);
  352. });
  353. function testFormat() {
  354. var validate = ajv.compile({ format: 'identifier' });
  355. validate('Abc1') .should.equal(true);
  356. validate('123') .should.equal(false);
  357. validate(123) .should.equal(true);
  358. }
  359. describe('formats for number', function() {
  360. it('should validate only numbers', function() {
  361. ajv.addFormat('positive', {
  362. type: 'number',
  363. validate: function(x) {
  364. return x > 0;
  365. }
  366. });
  367. var validate = ajv.compile({
  368. format: 'positive'
  369. });
  370. validate(-2) .should.equal(false);
  371. validate(0) .should.equal(false);
  372. validate(2) .should.equal(true);
  373. validate('abc') .should.equal(true);
  374. });
  375. it('should validate numbers with format via $data', function() {
  376. ajv = new Ajv({$data: true});
  377. ajv.addFormat('positive', {
  378. type: 'number',
  379. validate: function(x) {
  380. return x > 0;
  381. }
  382. });
  383. var validate = ajv.compile({
  384. properties: {
  385. data: { format: { $data: '1/frmt' } },
  386. frmt: { type: 'string' }
  387. }
  388. });
  389. validate({data: -2, frmt: 'positive'}) .should.equal(false);
  390. validate({data: 0, frmt: 'positive'}) .should.equal(false);
  391. validate({data: 2, frmt: 'positive'}) .should.equal(true);
  392. validate({data: 'abc', frmt: 'positive'}) .should.equal(true);
  393. });
  394. });
  395. });
  396. describe('validateSchema method', function() {
  397. it('should validate schema against meta-schema', function() {
  398. var valid = ajv.validateSchema({
  399. $schema: 'http://json-schema.org/draft-07/schema#',
  400. type: 'number'
  401. });
  402. valid .should.equal(true);
  403. should.equal(ajv.errors, null);
  404. valid = ajv.validateSchema({
  405. $schema: 'http://json-schema.org/draft-07/schema#',
  406. type: 'wrong_type'
  407. });
  408. valid .should.equal(false);
  409. ajv.errors.length .should.equal(3);
  410. ajv.errors[0].keyword .should.equal('enum');
  411. ajv.errors[1].keyword .should.equal('type');
  412. ajv.errors[2].keyword .should.equal('anyOf');
  413. });
  414. it('should throw exception if meta-schema is unknown', function() {
  415. should.throw(function() {
  416. ajv.validateSchema({
  417. $schema: 'http://example.com/unknown/schema#',
  418. type: 'number'
  419. });
  420. });
  421. });
  422. it('should throw exception if $schema is not a string', function() {
  423. should.throw(function() {
  424. ajv.validateSchema({
  425. $schema: {},
  426. type: 'number'
  427. });
  428. });
  429. });
  430. });
  431. });