errors.spec.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. 'use strict';
  2. var Ajv = require('./ajv')
  3. , should = require('./chai').should();
  4. describe('Validation errors', function () {
  5. var ajv, ajvJP, fullAjv;
  6. beforeEach(function() {
  7. createInstances();
  8. });
  9. function createInstances(errorDataPath) {
  10. ajv = new Ajv({ errorDataPath: errorDataPath, loopRequired: 21 });
  11. ajvJP = new Ajv({ errorDataPath: errorDataPath, jsonPointers: true, loopRequired: 21 });
  12. fullAjv = new Ajv({ errorDataPath: errorDataPath, allErrors: true, verbose: true, jsonPointers: true, loopRequired: 21 });
  13. }
  14. it('error should include dataPath', function() {
  15. var schema = {
  16. properties: {
  17. foo: { type: 'number' }
  18. }
  19. };
  20. testSchema1(schema);
  21. });
  22. it('"refs" error should include dataPath', function() {
  23. var schema = {
  24. definitions: {
  25. num: { type: 'number' }
  26. },
  27. properties: {
  28. foo: { $ref: '#/definitions/num' }
  29. }
  30. };
  31. testSchema1(schema, '#/definitions/num');
  32. });
  33. describe('"additionalProperties" errors', function() {
  34. it('should include property in dataPath with option errorDataPath="property"', function() {
  35. createInstances('property');
  36. testAdditional('property');
  37. });
  38. it('should NOT include property in dataPath WITHOUT option errorDataPath', function() {
  39. testAdditional();
  40. });
  41. function testAdditional(errorDataPath) {
  42. var schema = {
  43. properties: {
  44. foo: {},
  45. bar: {}
  46. },
  47. additionalProperties: false
  48. };
  49. var data = { foo: 1, bar: 2 }
  50. , invalidData = { foo: 1, bar: 2, baz: 3, quux: 4 };
  51. var path = pathFunc(errorDataPath);
  52. var msg = additionalFunc(errorDataPath);
  53. var validate = ajv.compile(schema);
  54. shouldBeValid(validate, data);
  55. shouldBeInvalid(validate, invalidData);
  56. shouldBeError(validate.errors[0], 'additionalProperties', '#/additionalProperties', path("['baz']"), msg, { additionalProperty: 'baz' });
  57. var validateJP = ajvJP.compile(schema);
  58. shouldBeValid(validateJP, data);
  59. shouldBeInvalid(validateJP, invalidData);
  60. shouldBeError(validateJP.errors[0], 'additionalProperties', '#/additionalProperties', path("/baz"), msg, { additionalProperty: 'baz' });
  61. var fullValidate = fullAjv.compile(schema);
  62. shouldBeValid(fullValidate, data);
  63. shouldBeInvalid(fullValidate, invalidData, 2);
  64. shouldBeError(fullValidate.errors[0], 'additionalProperties', '#/additionalProperties', path('/baz'), msg, { additionalProperty: 'baz' });
  65. shouldBeError(fullValidate.errors[1], 'additionalProperties', '#/additionalProperties', path('/quux'), msg, { additionalProperty: 'quux' });
  66. if (errorDataPath == 'property') {
  67. fullValidate.errors
  68. .filter(function(err) { return err.keyword == 'additionalProperties'; })
  69. .map(function(err) { return fullAjv._opts.jsonPointers ? err.dataPath.substr(1) : err.dataPath.slice(2,-2); })
  70. .forEach(function(p) { delete invalidData[p]; });
  71. invalidData .should.eql({ foo: 1, bar: 2 });
  72. }
  73. }
  74. });
  75. describe('errors when "additionalProperties" is schema', function() {
  76. it('should include property in dataPath with option errorDataPath="property"', function() {
  77. createInstances('property');
  78. testAdditionalIsSchema('property');
  79. });
  80. it('should NOT include property in dataPath WITHOUT option errorDataPath', function() {
  81. testAdditionalIsSchema();
  82. });
  83. function testAdditionalIsSchema() {
  84. var schema = {
  85. properties: {
  86. foo: { type: 'integer' },
  87. bar: { type: 'integer' }
  88. },
  89. additionalProperties: {
  90. type: 'object',
  91. properties: {
  92. quux: { type: 'string' }
  93. }
  94. }
  95. };
  96. var data = { foo: 1, bar: 2, baz: { quux: 'abc' } }
  97. , invalidData = { foo: 1, bar: 2, baz: { quux: 3 }, boo: { quux: 4 } };
  98. var schPath = '#/additionalProperties/properties/quux/type';
  99. var validate = ajv.compile(schema);
  100. shouldBeValid(validate, data);
  101. shouldBeInvalid(validate, invalidData);
  102. shouldBeError(validate.errors[0], 'type', schPath, "['baz'].quux", 'should be string', { type: 'string' });
  103. var validateJP = ajvJP.compile(schema);
  104. shouldBeValid(validateJP, data);
  105. shouldBeInvalid(validateJP, invalidData);
  106. shouldBeError(validateJP.errors[0], 'type', schPath, "/baz/quux", 'should be string', { type: 'string' });
  107. var fullValidate = fullAjv.compile(schema);
  108. shouldBeValid(fullValidate, data);
  109. shouldBeInvalid(fullValidate, invalidData, 2);
  110. shouldBeError(fullValidate.errors[0], 'type', schPath, '/baz/quux', 'should be string', { type: 'string' });
  111. shouldBeError(fullValidate.errors[1], 'type', schPath, '/boo/quux', 'should be string', { type: 'string' });
  112. }
  113. });
  114. describe('"required" errors', function() {
  115. it('should include missing property in dataPath with option errorDataPath="property"', function() {
  116. createInstances('property');
  117. testRequired('property');
  118. });
  119. it('should NOT include missing property in dataPath WITHOUT option errorDataPath', function() {
  120. testRequired();
  121. });
  122. function testRequired(errorDataPath) {
  123. var schema = {
  124. required: ['foo', 'bar', 'baz']
  125. };
  126. _testRequired(errorDataPath, schema, '#', '.');
  127. }
  128. it('large data/schemas with option errorDataPath="property"', function() {
  129. createInstances('property');
  130. testRequiredLargeSchema('property');
  131. });
  132. it('large data/schemas WITHOUT option errorDataPath', function() {
  133. testRequiredLargeSchema();
  134. });
  135. function testRequiredLargeSchema(errorDataPath) {
  136. var schema = { required: [] }
  137. , data = {}
  138. , invalidData1 = {}
  139. , invalidData2 = {};
  140. for (var i=0; i<100; i++) {
  141. schema.required.push(''+i); // properties from '0' to '99' are required
  142. data[i] = invalidData1[i] = invalidData2[i] = i;
  143. }
  144. delete invalidData1[1]; // property '1' will be missing
  145. delete invalidData2[2]; // properties '2' and '198' will be missing
  146. delete invalidData2[98];
  147. var path = pathFunc(errorDataPath);
  148. var msg = requiredFunc(errorDataPath);
  149. test();
  150. schema = { anyOf: [ schema ] };
  151. test(1, '#/anyOf/0');
  152. function test(extraErrors, schemaPathPrefix) {
  153. extraErrors = extraErrors || 0;
  154. var schPath = (schemaPathPrefix || '#') + '/required';
  155. var validate = ajv.compile(schema);
  156. shouldBeValid(validate, data);
  157. shouldBeInvalid(validate, invalidData1, 1 + extraErrors);
  158. shouldBeError(validate.errors[0], 'required', schPath, path("['1']"), msg('1'), { missingProperty: '1' });
  159. shouldBeInvalid(validate, invalidData2, 1 + extraErrors);
  160. shouldBeError(validate.errors[0], 'required', schPath, path("['2']"), msg('2'), { missingProperty: '2' });
  161. var validateJP = ajvJP.compile(schema);
  162. shouldBeValid(validateJP, data);
  163. shouldBeInvalid(validateJP, invalidData1, 1 + extraErrors);
  164. shouldBeError(validateJP.errors[0], 'required', schPath, path("/1"), msg('1'), { missingProperty: '1' });
  165. shouldBeInvalid(validateJP, invalidData2, 1 + extraErrors);
  166. shouldBeError(validateJP.errors[0], 'required', schPath, path("/2"), msg('2'), { missingProperty: '2' });
  167. var fullValidate = fullAjv.compile(schema);
  168. shouldBeValid(fullValidate, data);
  169. shouldBeInvalid(fullValidate, invalidData1, 1 + extraErrors);
  170. shouldBeError(fullValidate.errors[0], 'required', schPath, path('/1'), msg('1'), { missingProperty: '1' });
  171. shouldBeInvalid(fullValidate, invalidData2, 2 + extraErrors);
  172. shouldBeError(fullValidate.errors[0], 'required', schPath, path('/2'), msg('2'), { missingProperty: '2' });
  173. shouldBeError(fullValidate.errors[1], 'required', schPath, path('/98'), msg('98'), { missingProperty: '98' });
  174. }
  175. }
  176. it('with "properties" with option errorDataPath="property"', function() {
  177. createInstances('property');
  178. testRequiredAndProperties('property');
  179. });
  180. it('with "properties" WITHOUT option errorDataPath', function() {
  181. testRequiredAndProperties();
  182. });
  183. function testRequiredAndProperties(errorDataPath) {
  184. var schema = {
  185. properties: {
  186. 'foo': { type: 'number' },
  187. 'bar': { type: 'number' },
  188. 'baz': { type: 'number' },
  189. },
  190. required: ['foo', 'bar', 'baz']
  191. };
  192. _testRequired(errorDataPath, schema);
  193. }
  194. it('in "anyOf" with option errorDataPath="property"', function() {
  195. createInstances('property');
  196. testRequiredInAnyOf('property');
  197. });
  198. it('in "anyOf" WITHOUT option errorDataPath', function() {
  199. testRequiredInAnyOf();
  200. });
  201. function testRequiredInAnyOf(errorDataPath) {
  202. var schema = {
  203. anyOf: [
  204. { required: ['foo', 'bar', 'baz'] }
  205. ]
  206. };
  207. _testRequired(errorDataPath, schema, '#/anyOf/0', '.', 1);
  208. }
  209. it('should not validate required twice in large schemas with loopRequired option', function() {
  210. ajv = new Ajv({ loopRequired: 1, allErrors: true });
  211. var schema = {
  212. properties: {
  213. foo: { type: 'integer' },
  214. bar: { type: 'integer' }
  215. },
  216. required: ['foo', 'bar']
  217. };
  218. var validate = ajv.compile(schema);
  219. validate({}) .should.equal(false);
  220. validate.errors .should.have.length(2);
  221. });
  222. it('should not validate required twice with $data ref', function() {
  223. ajv = new Ajv({ $data: true, allErrors: true });
  224. var schema = {
  225. properties: {
  226. foo: { type: 'integer' },
  227. bar: { type: 'integer' }
  228. },
  229. required: { $data: '0/requiredProperties' }
  230. };
  231. var validate = ajv.compile(schema);
  232. validate({ requiredProperties: ['foo', 'bar'] }) .should.equal(false);
  233. validate.errors .should.have.length(2);
  234. });
  235. });
  236. describe('"dependencies" errors', function() {
  237. it('should include missing property in dataPath with option errorDataPath="property"', function() {
  238. createInstances('property');
  239. testDependencies('property');
  240. });
  241. it('should NOT include missing property in dataPath WITHOUT option errorDataPath', function() {
  242. testDependencies();
  243. });
  244. function testDependencies(errorDataPath) {
  245. var schema = {
  246. dependencies: {
  247. a: ['foo', 'bar', 'baz']
  248. }
  249. };
  250. var data = { a: 0, foo: 1, bar: 2, baz: 3 }
  251. , invalidData1 = { a: 0, foo: 1, baz: 3 }
  252. , invalidData2 = { a: 0, bar: 2 };
  253. var path = pathFunc(errorDataPath);
  254. var msg = 'should have properties foo, bar, baz when property a is present';
  255. var validate = ajv.compile(schema);
  256. shouldBeValid(validate, data);
  257. shouldBeInvalid(validate, invalidData1);
  258. shouldBeError(validate.errors[0], 'dependencies', '#/dependencies', path('.bar'), msg, params('.bar'));
  259. shouldBeInvalid(validate, invalidData2);
  260. shouldBeError(validate.errors[0], 'dependencies', '#/dependencies', path('.foo'), msg, params('.foo'));
  261. var validateJP = ajvJP.compile(schema);
  262. shouldBeValid(validateJP, data);
  263. shouldBeInvalid(validateJP, invalidData1);
  264. shouldBeError(validateJP.errors[0], 'dependencies', '#/dependencies', path('/bar'), msg, params('bar'));
  265. shouldBeInvalid(validateJP, invalidData2);
  266. shouldBeError(validateJP.errors[0], 'dependencies', '#/dependencies', path('/foo'), msg, params('foo'));
  267. var fullValidate = fullAjv.compile(schema);
  268. shouldBeValid(fullValidate, data);
  269. shouldBeInvalid(fullValidate, invalidData1);
  270. shouldBeError(fullValidate.errors[0], 'dependencies', '#/dependencies', path('/bar'), msg, params('bar'));
  271. shouldBeInvalid(fullValidate, invalidData2, 2);
  272. shouldBeError(fullValidate.errors[0], 'dependencies', '#/dependencies', path('/foo'), msg, params('foo'));
  273. shouldBeError(fullValidate.errors[1], 'dependencies', '#/dependencies', path('/baz'), msg, params('baz'));
  274. function params(missing) {
  275. var p = {
  276. property: 'a',
  277. deps: 'foo, bar, baz',
  278. depsCount: 3
  279. };
  280. p.missingProperty = missing;
  281. return p;
  282. }
  283. }
  284. });
  285. function _testRequired(errorDataPath, schema, schemaPathPrefix, prefix, extraErrors) {
  286. var schPath = (schemaPathPrefix || '#') + '/required';
  287. prefix = prefix || '';
  288. extraErrors = extraErrors || 0;
  289. var data = { foo: 1, bar: 2, baz: 3 }
  290. , invalidData1 = { foo: 1, baz: 3 }
  291. , invalidData2 = { bar: 2 };
  292. var path = pathFunc(errorDataPath);
  293. var msg = requiredFunc(errorDataPath);
  294. var validate = ajv.compile(schema);
  295. shouldBeValid(validate, data);
  296. shouldBeInvalid(validate, invalidData1, 1 + extraErrors);
  297. shouldBeError(validate.errors[0], 'required', schPath, path('.bar'), msg(prefix + 'bar'), { missingProperty: prefix + 'bar' });
  298. shouldBeInvalid(validate, invalidData2, 1 + extraErrors);
  299. shouldBeError(validate.errors[0], 'required', schPath, path('.foo'), msg(prefix + 'foo'), { missingProperty: prefix + 'foo' });
  300. var validateJP = ajvJP.compile(schema);
  301. shouldBeValid(validateJP, data);
  302. shouldBeInvalid(validateJP, invalidData1, 1 + extraErrors);
  303. shouldBeError(validateJP.errors[0], 'required', schPath, path('/bar'), msg('bar'), { missingProperty: 'bar' });
  304. shouldBeInvalid(validateJP, invalidData2, 1 + extraErrors);
  305. shouldBeError(validateJP.errors[0], 'required', schPath, path('/foo'), msg('foo'), { missingProperty: 'foo' });
  306. var fullValidate = fullAjv.compile(schema);
  307. shouldBeValid(fullValidate, data);
  308. shouldBeInvalid(fullValidate, invalidData1, 1 + extraErrors);
  309. shouldBeError(fullValidate.errors[0], 'required', schPath, path('/bar'), msg('bar'), { missingProperty: 'bar' });
  310. shouldBeInvalid(fullValidate, invalidData2, 2 + extraErrors);
  311. shouldBeError(fullValidate.errors[0], 'required', schPath, path('/foo'), msg('foo'), { missingProperty: 'foo' });
  312. shouldBeError(fullValidate.errors[1], 'required', schPath, path('/baz'), msg('baz'), { missingProperty: 'baz' });
  313. }
  314. function pathFunc(errorDataPath) {
  315. return function (dataPath) {
  316. return errorDataPath == 'property' ? dataPath : '';
  317. };
  318. }
  319. function requiredFunc(errorDataPath) {
  320. return function (prop) {
  321. return errorDataPath == 'property'
  322. ? 'is a required property'
  323. : 'should have required property \'' + prop + '\'';
  324. };
  325. }
  326. function additionalFunc(errorDataPath) {
  327. return errorDataPath == 'property'
  328. ? 'is an invalid additional property'
  329. : 'should NOT have additional properties';
  330. }
  331. it('"items" errors should include item index without quotes in dataPath (#48)', function() {
  332. var schema1 = {
  333. $id: 'schema1',
  334. type: 'array',
  335. items: {
  336. type: 'integer',
  337. minimum: 10
  338. }
  339. };
  340. var data = [ 10, 11, 12]
  341. , invalidData1 = [ 1, 10 ]
  342. , invalidData2 = [ 10, 9, 11, 8, 12];
  343. var validate = ajv.compile(schema1);
  344. shouldBeValid(validate, data);
  345. shouldBeInvalid(validate, invalidData1);
  346. shouldBeError(validate.errors[0], 'minimum', '#/items/minimum', '[0]', 'should be >= 10');
  347. shouldBeInvalid(validate, invalidData2);
  348. shouldBeError(validate.errors[0], 'minimum', '#/items/minimum', '[1]', 'should be >= 10');
  349. var validateJP = ajvJP.compile(schema1);
  350. shouldBeValid(validateJP, data);
  351. shouldBeInvalid(validateJP, invalidData1);
  352. shouldBeError(validateJP.errors[0], 'minimum', '#/items/minimum', '/0', 'should be >= 10');
  353. shouldBeInvalid(validateJP, invalidData2);
  354. shouldBeError(validateJP.errors[0], 'minimum', '#/items/minimum', '/1', 'should be >= 10');
  355. var fullValidate = fullAjv.compile(schema1);
  356. shouldBeValid(fullValidate, data);
  357. shouldBeInvalid(fullValidate, invalidData1);
  358. shouldBeError(fullValidate.errors[0], 'minimum', '#/items/minimum', '/0', 'should be >= 10');
  359. shouldBeInvalid(fullValidate, invalidData2, 2);
  360. shouldBeError(fullValidate.errors[0], 'minimum', '#/items/minimum', '/1', 'should be >= 10');
  361. shouldBeError(fullValidate.errors[1], 'minimum', '#/items/minimum', '/3', 'should be >= 10');
  362. var schema2 = {
  363. $id: 'schema2',
  364. type: 'array',
  365. items: [{ minimum: 10 }, { minimum: 9 }, { minimum: 12 }]
  366. };
  367. validate = ajv.compile(schema2);
  368. shouldBeValid(validate, data);
  369. shouldBeInvalid(validate, invalidData1);
  370. shouldBeError(validate.errors[0], 'minimum', '#/items/0/minimum', '[0]', 'should be >= 10');
  371. shouldBeInvalid(validate, invalidData2);
  372. shouldBeError(validate.errors[0], 'minimum', '#/items/2/minimum', '[2]', 'should be >= 12');
  373. });
  374. it('should have correct schema path for additionalItems', function() {
  375. var schema = {
  376. type: 'array',
  377. items: [ { type: 'integer' }, { type: 'integer' } ],
  378. additionalItems: false
  379. };
  380. var data = [ 1, 2 ]
  381. , invalidData = [ 1, 2, 3 ];
  382. test(ajv);
  383. test(ajvJP);
  384. test(fullAjv);
  385. function test(_ajv) {
  386. var validate = _ajv.compile(schema);
  387. shouldBeValid(validate, data);
  388. shouldBeInvalid(validate, invalidData);
  389. shouldBeError(validate.errors[0], 'additionalItems', '#/additionalItems', '', 'should NOT have more than 2 items');
  390. }
  391. });
  392. describe('"propertyNames" errors', function() {
  393. it('should add propertyName to errors', function() {
  394. var schema = {
  395. type: 'object',
  396. propertyNames: { format: 'email' }
  397. };
  398. var data = {
  399. 'bar.baz@email.example.com': {}
  400. };
  401. var invalidData = {
  402. 'foo': {},
  403. 'bar': {},
  404. 'bar.baz@email.example.com': {}
  405. };
  406. test(ajv, 2);
  407. test(ajvJP, 2);
  408. test(fullAjv, 4);
  409. function test(_ajv, numErrors) {
  410. var validate = _ajv.compile(schema);
  411. shouldBeValid(validate, data);
  412. shouldBeInvalid(validate, invalidData, numErrors);
  413. shouldBeError(validate.errors[0], 'format', '#/propertyNames/format', '', 'should match format "email"');
  414. shouldBeError(validate.errors[1], 'propertyNames', '#/propertyNames', '', 'property name \'foo\' is invalid');
  415. if (numErrors == 4) {
  416. shouldBeError(validate.errors[2], 'format', '#/propertyNames/format', '', 'should match format "email"');
  417. shouldBeError(validate.errors[3], 'propertyNames', '#/propertyNames', '', 'property name \'bar\' is invalid');
  418. }
  419. }
  420. });
  421. });
  422. describe('oneOf errors', function() {
  423. it('should have errors from inner schemas', function() {
  424. var schema = {
  425. oneOf: [
  426. { type: 'number' },
  427. { type: 'integer' }
  428. ]
  429. };
  430. test(ajv);
  431. test(fullAjv);
  432. function test(_ajv) {
  433. var validate = _ajv.compile(schema);
  434. validate('foo') .should.equal(false);
  435. validate.errors.length .should.equal(3);
  436. validate(1) .should.equal(false);
  437. validate.errors.length .should.equal(1);
  438. validate(1.5) .should.equal(true);
  439. }
  440. });
  441. it('should return passing schemas in error params', function() {
  442. var schema = {
  443. oneOf: [
  444. { type: 'number' },
  445. { type: 'integer' },
  446. { const: 1.5 }
  447. ]
  448. };
  449. test(ajv);
  450. test(fullAjv);
  451. function test(_ajv) {
  452. var validate = _ajv.compile(schema);
  453. validate(1) .should.equal(false);
  454. var err = validate.errors.pop();
  455. err.keyword .should.equal('oneOf');
  456. err.params .should.eql({passingSchemas: [0, 1]});
  457. validate(1.5) .should.equal(false);
  458. err = validate.errors.pop();
  459. err.keyword .should.equal('oneOf');
  460. err.params .should.eql({passingSchemas: [0, 2]});
  461. validate(2.5) .should.equal(true);
  462. validate('foo') .should.equal(false);
  463. err = validate.errors.pop();
  464. err.keyword .should.equal('oneOf');
  465. err.params .should.eql({passingSchemas: null});
  466. }
  467. });
  468. });
  469. describe('anyOf errors', function() {
  470. it('should have errors from inner schemas', function() {
  471. var schema = {
  472. anyOf: [
  473. { type: 'number' },
  474. { type: 'integer' }
  475. ]
  476. };
  477. test(ajv);
  478. test(fullAjv);
  479. function test(_ajv) {
  480. var validate = _ajv.compile(schema);
  481. validate('foo') .should.equal(false);
  482. validate.errors.length .should.equal(3);
  483. validate(1) .should.equal(true);
  484. validate(1.5) .should.equal(true);
  485. }
  486. });
  487. });
  488. describe('type errors', function() {
  489. describe('integer', function() {
  490. it('should have only one error in {allErrors: false} mode', function() {
  491. test(ajv);
  492. });
  493. it('should return all errors in {allErrors: true} mode', function() {
  494. test(fullAjv, 2);
  495. });
  496. function test(_ajv, numErrors) {
  497. var schema = {
  498. type: 'integer',
  499. minimum: 5
  500. };
  501. var validate = _ajv.compile(schema);
  502. shouldBeValid(validate, 5);
  503. shouldBeInvalid(validate, 5.5);
  504. shouldBeInvalid(validate, 4);
  505. shouldBeInvalid(validate, '4');
  506. shouldBeInvalid(validate, 4.5, numErrors);
  507. }
  508. });
  509. describe('keyword for another type', function() {
  510. it('should have only one error in {allErrors: false} mode', function() {
  511. test(ajv);
  512. });
  513. it('should return all errors in {allErrors: true} mode', function() {
  514. test(fullAjv, 2);
  515. });
  516. function test(_ajv, numErrors) {
  517. var schema = {
  518. type: 'array',
  519. minItems: 2,
  520. minimum: 5
  521. };
  522. var validate = _ajv.compile(schema);
  523. shouldBeValid(validate, [1, 2]);
  524. shouldBeInvalid(validate, [1]);
  525. shouldBeInvalid(validate, 5);
  526. shouldBeInvalid(validate, 4, numErrors);
  527. }
  528. });
  529. describe('array of types', function() {
  530. it('should have only one error in {allErrors: false} mode', function() {
  531. test(ajv);
  532. });
  533. it('should return all errors in {allErrors: true} mode', function() {
  534. test(fullAjv, 2);
  535. });
  536. function test(_ajv, numErrors) {
  537. var schema = {
  538. type: ['array', 'object'],
  539. minItems: 2,
  540. minProperties: 2,
  541. minimum: 5
  542. };
  543. var validate = _ajv.compile(schema);
  544. shouldBeValid(validate, [1, 2]);
  545. shouldBeValid(validate, {foo: 1, bar: 2});
  546. shouldBeInvalid(validate, [1]);
  547. shouldBeInvalid(validate, {foo: 1});
  548. shouldBeInvalid(validate, 5);
  549. shouldBeInvalid(validate, 4, numErrors);
  550. }
  551. });
  552. });
  553. describe('exclusiveMaximum/Minimum errors', function() {
  554. it('should include limits in error message', function() {
  555. var schema = {
  556. type: 'integer',
  557. exclusiveMinimum: 2,
  558. exclusiveMaximum: 5
  559. };
  560. [ajv, fullAjv].forEach(function (_ajv) {
  561. var validate = _ajv.compile(schema);
  562. shouldBeValid(validate, 3);
  563. shouldBeValid(validate, 4);
  564. shouldBeInvalid(validate, 2);
  565. testError('exclusiveMinimum', 'should be > 2', {comparison: '>', limit: 2, exclusive: true});
  566. shouldBeInvalid(validate, 5);
  567. testError('exclusiveMaximum', 'should be < 5', {comparison: '<', limit: 5, exclusive: true});
  568. function testError(keyword, message, params) {
  569. var err = validate.errors[0];
  570. shouldBeError(err, keyword, '#/' + keyword, '', message, params);
  571. }
  572. });
  573. });
  574. it('should include limits in error message with $data', function() {
  575. var schema = {
  576. "properties": {
  577. "smaller": {
  578. "type": "number",
  579. "exclusiveMaximum": { "$data": "1/larger" }
  580. },
  581. "larger": { "type": "number" }
  582. }
  583. };
  584. ajv = new Ajv({$data: true});
  585. fullAjv = new Ajv({$data: true, allErrors: true, verbose: true, jsonPointers: true});
  586. [ajv, fullAjv].forEach(function (_ajv) {
  587. var validate = _ajv.compile(schema);
  588. shouldBeValid(validate, {smaller: 2, larger: 4});
  589. shouldBeValid(validate, {smaller: 3, larger: 4});
  590. shouldBeInvalid(validate, {smaller: 4, larger: 4});
  591. testError();
  592. shouldBeInvalid(validate, {smaller: 5, larger: 4});
  593. testError();
  594. function testError() {
  595. var err = validate.errors[0];
  596. shouldBeError(err, 'exclusiveMaximum',
  597. '#/properties/smaller/exclusiveMaximum',
  598. _ajv._opts.jsonPointers ? '/smaller' : '.smaller',
  599. 'should be < 4',
  600. {comparison: '<', limit: 4, exclusive: true});
  601. }
  602. });
  603. });
  604. });
  605. describe('if/then/else errors', function() {
  606. var validate, numErrors;
  607. it('if/then/else should include failing keyword in message and params', function() {
  608. var schema = {
  609. 'if': { maximum: 10 },
  610. 'then': { multipleOf: 2 },
  611. 'else': { multipleOf: 5 }
  612. };
  613. [ajv, fullAjv].forEach(function (_ajv) {
  614. prepareTest(_ajv, schema);
  615. shouldBeValid(validate, 8);
  616. shouldBeValid(validate, 15);
  617. shouldBeInvalid(validate, 7, numErrors);
  618. testIfError('then', 2);
  619. shouldBeInvalid(validate, 17, numErrors);
  620. testIfError('else', 5);
  621. });
  622. });
  623. it('if/then should include failing keyword in message and params', function() {
  624. var schema = {
  625. 'if': { maximum: 10 },
  626. 'then': { multipleOf: 2 }
  627. };
  628. [ajv, fullAjv].forEach(function (_ajv) {
  629. prepareTest(_ajv, schema);
  630. shouldBeValid(validate, 8);
  631. shouldBeValid(validate, 11);
  632. shouldBeValid(validate, 12);
  633. shouldBeInvalid(validate, 7, numErrors);
  634. testIfError('then', 2);
  635. });
  636. });
  637. it('if/else should include failing keyword in message and params', function() {
  638. var schema = {
  639. 'if': { maximum: 10 },
  640. 'else': { multipleOf: 5 }
  641. };
  642. [ajv, fullAjv].forEach(function (_ajv) {
  643. prepareTest(_ajv, schema);
  644. shouldBeValid(validate, 7);
  645. shouldBeValid(validate, 8);
  646. shouldBeValid(validate, 15);
  647. shouldBeInvalid(validate, 17, numErrors);
  648. testIfError('else', 5);
  649. });
  650. });
  651. function prepareTest(_ajv, schema) {
  652. validate = _ajv.compile(schema);
  653. numErrors = _ajv._opts.allErrors ? 2 : 1;
  654. }
  655. function testIfError(ifClause, multipleOf) {
  656. var err = validate.errors[0];
  657. shouldBeError(err, 'multipleOf', '#/' + ifClause + '/multipleOf', '',
  658. 'should be multiple of ' + multipleOf, {multipleOf: multipleOf});
  659. if (numErrors == 2) {
  660. err = validate.errors[1];
  661. shouldBeError(err, 'if', '#/if', '',
  662. 'should match "' + ifClause + '" schema', {failingKeyword: ifClause});
  663. }
  664. }
  665. });
  666. describe('uniqueItems errors', function() {
  667. it('should not return uniqueItems error when non-unique items are of a different type than required', function() {
  668. var schema = {
  669. items: {type: 'number'},
  670. uniqueItems: true
  671. };
  672. [ajvJP, fullAjv].forEach(function (_ajv) {
  673. var validate = _ajv.compile(schema);
  674. shouldBeValid(validate, [1, 2, 3]);
  675. shouldBeInvalid(validate, [1, 2, 2]);
  676. shouldBeError(validate.errors[0], 'uniqueItems', '#/uniqueItems', '',
  677. 'should NOT have duplicate items (items ## 2 and 1 are identical)',
  678. {i: 1, j: 2});
  679. var expectedErrors = _ajv._opts.allErrors ? 2 : 1;
  680. shouldBeInvalid(validate, [1, "2", "2", 2], expectedErrors);
  681. testTypeError(0, '/1');
  682. if (expectedErrors == 2) testTypeError(1, '/2');
  683. function testTypeError(i, dataPath) {
  684. var err = validate.errors[i];
  685. shouldBeError(err, 'type', '#/items/type', dataPath, 'should be number');
  686. }
  687. });
  688. });
  689. });
  690. function testSchema1(schema, schemaPathPrefix) {
  691. _testSchema1(ajv, schema, schemaPathPrefix);
  692. _testSchema1(ajvJP, schema, schemaPathPrefix);
  693. _testSchema1(fullAjv, schema, schemaPathPrefix);
  694. }
  695. function _testSchema1(_ajv, schema, schemaPathPrefix) {
  696. var schPath = (schemaPathPrefix || '#/properties/foo') + '/type';
  697. var data = { foo: 1 }
  698. , invalidData = { foo: 'bar' };
  699. var validate = _ajv.compile(schema);
  700. shouldBeValid(validate, data);
  701. shouldBeInvalid(validate, invalidData);
  702. shouldBeError(validate.errors[0], 'type', schPath, _ajv._opts.jsonPointers ? '/foo' : '.foo');
  703. }
  704. function shouldBeValid(validate, data) {
  705. validate(data) .should.equal(true);
  706. should.equal(validate.errors, null);
  707. }
  708. function shouldBeInvalid(validate, data, numErrors) {
  709. validate(data) .should.equal(false);
  710. should.equal(validate.errors.length, numErrors || 1);
  711. }
  712. function shouldBeError(error, keyword, schemaPath, dataPath, message, params) {
  713. error.keyword .should.equal(keyword);
  714. error.schemaPath .should.equal(schemaPath);
  715. error.dataPath .should.equal(dataPath);
  716. error.message .should.be.a('string');
  717. if (message !== undefined) error.message .should.equal(message);
  718. if (params !== undefined) error.params .should.eql(params);
  719. }
  720. });