misc.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. var tape = require('tape')
  2. var cosmic = require('./fixtures/cosmic')
  3. var validator = require('../')
  4. var validatorRequire = require('../require')
  5. tape('simple', function(t) {
  6. var schema = {
  7. required: true,
  8. type: 'object',
  9. properties: {
  10. hello: {type:'string', required:true}
  11. }
  12. }
  13. var validate = validator(schema)
  14. t.ok(validate({hello: 'world'}), 'should be valid')
  15. t.notOk(validate(), 'should be invalid')
  16. t.notOk(validate({}), 'should be invalid')
  17. t.end()
  18. })
  19. tape('advanced', function(t) {
  20. var validate = validator(cosmic.schema)
  21. t.ok(validate(cosmic.valid), 'should be valid')
  22. t.notOk(validate(cosmic.invalid), 'should be invalid')
  23. t.end()
  24. })
  25. tape('greedy/false', function(t) {
  26. var validate = validator({
  27. type: 'object',
  28. properties: {
  29. x: {
  30. type: 'number'
  31. }
  32. },
  33. required: ['x', 'y']
  34. });
  35. t.notOk(validate({}), 'should be invalid')
  36. t.strictEqual(validate.errors.length, 2);
  37. t.strictEqual(validate.errors[0].field, 'data.x')
  38. t.strictEqual(validate.errors[0].message, 'is required')
  39. t.strictEqual(validate.errors[1].field, 'data.y')
  40. t.strictEqual(validate.errors[1].message, 'is required')
  41. t.notOk(validate({x: 'string'}), 'should be invalid')
  42. t.strictEqual(validate.errors.length, 1);
  43. t.strictEqual(validate.errors[0].field, 'data.y')
  44. t.strictEqual(validate.errors[0].message, 'is required')
  45. t.notOk(validate({x: 'string', y: 'value'}), 'should be invalid')
  46. t.strictEqual(validate.errors.length, 1);
  47. t.strictEqual(validate.errors[0].field, 'data.x')
  48. t.strictEqual(validate.errors[0].message, 'is the wrong type')
  49. t.end();
  50. });
  51. tape('greedy/true', function(t) {
  52. var validate = validator({
  53. type: 'object',
  54. properties: {
  55. x: {
  56. type: 'number'
  57. }
  58. },
  59. required: ['x', 'y']
  60. }, {
  61. greedy: true
  62. });
  63. t.notOk(validate({}), 'should be invalid')
  64. t.strictEqual(validate.errors.length, 2);
  65. t.strictEqual(validate.errors[0].field, 'data.x')
  66. t.strictEqual(validate.errors[0].message, 'is required')
  67. t.strictEqual(validate.errors[1].field, 'data.y')
  68. t.strictEqual(validate.errors[1].message, 'is required')
  69. t.notOk(validate({x: 'string'}), 'should be invalid')
  70. t.strictEqual(validate.errors.length, 2);
  71. t.strictEqual(validate.errors[0].field, 'data.y')
  72. t.strictEqual(validate.errors[0].message, 'is required')
  73. t.strictEqual(validate.errors[1].field, 'data.x')
  74. t.strictEqual(validate.errors[1].message, 'is the wrong type')
  75. t.notOk(validate({x: 'string', y: 'value'}), 'should be invalid')
  76. t.strictEqual(validate.errors.length, 1);
  77. t.strictEqual(validate.errors[0].field, 'data.x')
  78. t.strictEqual(validate.errors[0].message, 'is the wrong type')
  79. t.ok(validate({x: 1, y: 'value'}), 'should be invalid')
  80. t.end();
  81. });
  82. tape('additional props', function(t) {
  83. var validate = validator({
  84. type: 'object',
  85. additionalProperties: false
  86. }, {
  87. verbose: true
  88. })
  89. t.ok(validate({}))
  90. t.notOk(validate({foo:'bar'}))
  91. t.ok(validate.errors[0].value === 'data.foo', 'should output the property not allowed in verbose mode')
  92. t.end()
  93. })
  94. tape('array', function(t) {
  95. var validate = validator({
  96. type: 'array',
  97. required: true,
  98. items: {
  99. type: 'string'
  100. }
  101. })
  102. t.notOk(validate({}), 'wrong type')
  103. t.notOk(validate(), 'is required')
  104. t.ok(validate(['test']))
  105. t.end()
  106. })
  107. tape('nested array', function(t) {
  108. var validate = validator({
  109. type: 'object',
  110. properties: {
  111. list: {
  112. type: 'array',
  113. required: true,
  114. items: {
  115. type: 'string'
  116. }
  117. }
  118. }
  119. })
  120. t.notOk(validate({}), 'is required')
  121. t.ok(validate({list:['test']}))
  122. t.notOk(validate({list:[1]}))
  123. t.end()
  124. })
  125. tape('enum', function(t) {
  126. var validate = validator({
  127. type: 'object',
  128. properties: {
  129. foo: {
  130. type: 'number',
  131. required: true,
  132. enum: [42]
  133. }
  134. }
  135. })
  136. t.notOk(validate({}), 'is required')
  137. t.ok(validate({foo:42}))
  138. t.notOk(validate({foo:43}))
  139. t.end()
  140. })
  141. tape('minimum/maximum', function(t) {
  142. var validate = validator({
  143. type: 'object',
  144. properties: {
  145. foo: {
  146. type: 'number',
  147. minimum: 0,
  148. maximum: 0
  149. }
  150. }
  151. })
  152. t.notOk(validate({foo:-42}))
  153. t.ok(validate({foo:0}))
  154. t.notOk(validate({foo:42}))
  155. t.end()
  156. })
  157. tape('exclusiveMinimum/exclusiveMaximum', function(t) {
  158. var validate = validator({
  159. type: 'object',
  160. properties: {
  161. foo: {
  162. type: 'number',
  163. minimum: 10,
  164. maximum: 20,
  165. exclusiveMinimum: true,
  166. exclusiveMaximum: true
  167. }
  168. }
  169. })
  170. t.notOk(validate({foo:10}))
  171. t.ok(validate({foo:11}))
  172. t.notOk(validate({foo:20}))
  173. t.ok(validate({foo:19}))
  174. t.end()
  175. })
  176. tape('custom format', function(t) {
  177. var validate = validator({
  178. type: 'object',
  179. properties: {
  180. foo: {
  181. type: 'string',
  182. format: 'as'
  183. }
  184. }
  185. }, {formats: {as:/^a+$/}})
  186. t.notOk(validate({foo:''}), 'not as')
  187. t.notOk(validate({foo:'b'}), 'not as')
  188. t.notOk(validate({foo:'aaab'}), 'not as')
  189. t.ok(validate({foo:'a'}), 'as')
  190. t.ok(validate({foo:'aaaaaa'}), 'as')
  191. t.end()
  192. })
  193. tape('custom format function', function(t) {
  194. var validate = validator({
  195. type: 'object',
  196. properties: {
  197. foo: {
  198. type: 'string',
  199. format: 'as'
  200. }
  201. }
  202. }, {formats: {as:function(s) { return /^a+$/.test(s) } }})
  203. t.notOk(validate({foo:''}), 'not as')
  204. t.notOk(validate({foo:'b'}), 'not as')
  205. t.notOk(validate({foo:'aaab'}), 'not as')
  206. t.ok(validate({foo:'a'}), 'as')
  207. t.ok(validate({foo:'aaaaaa'}), 'as')
  208. t.end()
  209. })
  210. tape('do not mutate schema', function(t) {
  211. var sch = {
  212. items: [
  213. {}
  214. ],
  215. additionalItems: {
  216. type: 'integer'
  217. }
  218. }
  219. var copy = JSON.parse(JSON.stringify(sch))
  220. validator(sch)
  221. t.same(sch, copy, 'did not mutate')
  222. t.end()
  223. })
  224. tape('#toJSON()', function(t) {
  225. var schema = {
  226. required: true,
  227. type: 'object',
  228. properties: {
  229. hello: {type:'string', required:true}
  230. }
  231. }
  232. var validate = validator(schema)
  233. t.deepEqual(validate.toJSON(), schema, 'should return original schema')
  234. t.end()
  235. })
  236. tape('external schemas', function(t) {
  237. var ext = {type: 'string'}
  238. var schema = {
  239. required: true,
  240. $ref: '#ext'
  241. }
  242. var validate = validator(schema, {schemas: {ext:ext}})
  243. t.ok(validate('hello string'), 'is a string')
  244. t.notOk(validate(42), 'not a string')
  245. t.end()
  246. })
  247. tape('top-level external schema', function(t) {
  248. var defs = {
  249. "string": {
  250. type: "string"
  251. },
  252. "sex": {
  253. type: "string",
  254. enum: ["male", "female", "other"]
  255. }
  256. }
  257. var schema = {
  258. type: "object",
  259. properties: {
  260. "name": { $ref: "definitions.json#/string" },
  261. "sex": { $ref: "definitions.json#/sex" }
  262. },
  263. required: ["name", "sex"]
  264. }
  265. var validate = validator(schema, {
  266. schemas: {
  267. "definitions.json": defs
  268. }
  269. })
  270. t.ok(validate({name:"alice", sex:"female"}), 'is an object')
  271. t.notOk(validate({name:"alice", sex: "bob"}), 'recognizes external schema')
  272. t.notOk(validate({name:2, sex: "female"}), 'recognizes external schema')
  273. t.end()
  274. })
  275. tape('nested required array decl', function(t) {
  276. var schema = {
  277. properties: {
  278. x: {
  279. type: 'object',
  280. properties: {
  281. y: {
  282. type: 'object',
  283. properties: {
  284. z: {
  285. type: 'string'
  286. }
  287. },
  288. required: ['z']
  289. }
  290. }
  291. }
  292. },
  293. required: ['x']
  294. }
  295. var validate = validator(schema)
  296. t.ok(validate({x: {}}), 'should be valid')
  297. t.notOk(validate({}), 'should not be valid')
  298. t.strictEqual(validate.errors[0].field, 'data.x', 'should output the missing field')
  299. t.end()
  300. })
  301. tape('verbose mode', function(t) {
  302. var schema = {
  303. required: true,
  304. type: 'object',
  305. properties: {
  306. hello: {
  307. required: true,
  308. type: 'string'
  309. }
  310. }
  311. };
  312. var validate = validator(schema, {verbose: true})
  313. t.ok(validate({hello: 'string'}), 'should be valid')
  314. t.notOk(validate({hello: 100}), 'should not be valid')
  315. t.strictEqual(validate.errors[0].value, 100, 'error object should contain the invalid value')
  316. t.end()
  317. })
  318. tape('additional props in verbose mode', function(t) {
  319. var schema = {
  320. type: 'object',
  321. required: true,
  322. additionalProperties: false,
  323. properties: {
  324. foo: {
  325. type: 'string'
  326. },
  327. 'hello world': {
  328. type: 'object',
  329. required: true,
  330. additionalProperties: false,
  331. properties: {
  332. foo: {
  333. type: 'string'
  334. }
  335. }
  336. }
  337. }
  338. };
  339. var validate = validator(schema, {verbose: true})
  340. validate({'hello world': {bar: 'string'}});
  341. t.strictEqual(validate.errors[0].value, 'data["hello world"].bar', 'should output the path to the additional prop in the error')
  342. t.end()
  343. })
  344. tape('Date.now() is an integer', function(t) {
  345. var schema = {type: 'integer'}
  346. var validate = validator(schema)
  347. t.ok(validate(Date.now()), 'is integer')
  348. t.end()
  349. })
  350. tape('field shows item index in arrays', function(t) {
  351. var schema = {
  352. type: 'array',
  353. items: {
  354. type: 'array',
  355. items: {
  356. properties: {
  357. foo: {
  358. type: 'string',
  359. required: true
  360. }
  361. }
  362. }
  363. }
  364. }
  365. var validate = validator(schema)
  366. validate([
  367. [
  368. { foo: 'test' },
  369. { foo: 'test' }
  370. ],
  371. [
  372. { foo: 'test' },
  373. { baz: 'test' }
  374. ]
  375. ])
  376. t.strictEqual(validate.errors[0].field, 'data.1.1.foo', 'should output the field with specific index of failing item in the error')
  377. t.end()
  378. })