react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / har-validator / node_modules / is-my-json-valid / test / misc.js
81155 viewsvar tape = require('tape')1var cosmic = require('./fixtures/cosmic')2var validator = require('../')3var validatorRequire = require('../require')45tape('simple', function(t) {6var schema = {7required: true,8type: 'object',9properties: {10hello: {type:'string', required:true}11}12}1314var validate = validator(schema)1516t.ok(validate({hello: 'world'}), 'should be valid')17t.notOk(validate(), 'should be invalid')18t.notOk(validate({}), 'should be invalid')19t.end()20})2122tape('advanced', function(t) {23var validate = validator(cosmic.schema)2425t.ok(validate(cosmic.valid), 'should be valid')26t.notOk(validate(cosmic.invalid), 'should be invalid')27t.end()28})2930tape('greedy/false', function(t) {31var validate = validator({32type: 'object',33properties: {34x: {35type: 'number'36}37},38required: ['x', 'y']39});40t.notOk(validate({}), 'should be invalid')41t.strictEqual(validate.errors.length, 2);42t.strictEqual(validate.errors[0].field, 'data.x')43t.strictEqual(validate.errors[0].message, 'is required')44t.strictEqual(validate.errors[1].field, 'data.y')45t.strictEqual(validate.errors[1].message, 'is required')46t.notOk(validate({x: 'string'}), 'should be invalid')47t.strictEqual(validate.errors.length, 1);48t.strictEqual(validate.errors[0].field, 'data.y')49t.strictEqual(validate.errors[0].message, 'is required')50t.notOk(validate({x: 'string', y: 'value'}), 'should be invalid')51t.strictEqual(validate.errors.length, 1);52t.strictEqual(validate.errors[0].field, 'data.x')53t.strictEqual(validate.errors[0].message, 'is the wrong type')54t.end();55});5657tape('greedy/true', function(t) {58var validate = validator({59type: 'object',60properties: {61x: {62type: 'number'63}64},65required: ['x', 'y']66}, {67greedy: true68});69t.notOk(validate({}), 'should be invalid')70t.strictEqual(validate.errors.length, 2);71t.strictEqual(validate.errors[0].field, 'data.x')72t.strictEqual(validate.errors[0].message, 'is required')73t.strictEqual(validate.errors[1].field, 'data.y')74t.strictEqual(validate.errors[1].message, 'is required')75t.notOk(validate({x: 'string'}), 'should be invalid')76t.strictEqual(validate.errors.length, 2);77t.strictEqual(validate.errors[0].field, 'data.y')78t.strictEqual(validate.errors[0].message, 'is required')79t.strictEqual(validate.errors[1].field, 'data.x')80t.strictEqual(validate.errors[1].message, 'is the wrong type')81t.notOk(validate({x: 'string', y: 'value'}), 'should be invalid')82t.strictEqual(validate.errors.length, 1);83t.strictEqual(validate.errors[0].field, 'data.x')84t.strictEqual(validate.errors[0].message, 'is the wrong type')85t.ok(validate({x: 1, y: 'value'}), 'should be invalid')86t.end();87});8889tape('additional props', function(t) {90var validate = validator({91type: 'object',92additionalProperties: false93}, {94verbose: true95})9697t.ok(validate({}))98t.notOk(validate({foo:'bar'}))99t.ok(validate.errors[0].value === 'data.foo', 'should output the property not allowed in verbose mode')100t.end()101})102103tape('array', function(t) {104var validate = validator({105type: 'array',106required: true,107items: {108type: 'string'109}110})111112t.notOk(validate({}), 'wrong type')113t.notOk(validate(), 'is required')114t.ok(validate(['test']))115t.end()116})117118tape('nested array', function(t) {119var validate = validator({120type: 'object',121properties: {122list: {123type: 'array',124required: true,125items: {126type: 'string'127}128}129}130})131132t.notOk(validate({}), 'is required')133t.ok(validate({list:['test']}))134t.notOk(validate({list:[1]}))135t.end()136})137138tape('enum', function(t) {139var validate = validator({140type: 'object',141properties: {142foo: {143type: 'number',144required: true,145enum: [42]146}147}148})149150t.notOk(validate({}), 'is required')151t.ok(validate({foo:42}))152t.notOk(validate({foo:43}))153t.end()154})155156tape('minimum/maximum', function(t) {157var validate = validator({158type: 'object',159properties: {160foo: {161type: 'number',162minimum: 0,163maximum: 0164}165}166})167168t.notOk(validate({foo:-42}))169t.ok(validate({foo:0}))170t.notOk(validate({foo:42}))171t.end()172})173174tape('exclusiveMinimum/exclusiveMaximum', function(t) {175var validate = validator({176type: 'object',177properties: {178foo: {179type: 'number',180minimum: 10,181maximum: 20,182exclusiveMinimum: true,183exclusiveMaximum: true184}185}186})187188t.notOk(validate({foo:10}))189t.ok(validate({foo:11}))190t.notOk(validate({foo:20}))191t.ok(validate({foo:19}))192t.end()193})194195tape('custom format', function(t) {196var validate = validator({197type: 'object',198properties: {199foo: {200type: 'string',201format: 'as'202}203}204}, {formats: {as:/^a+$/}})205206t.notOk(validate({foo:''}), 'not as')207t.notOk(validate({foo:'b'}), 'not as')208t.notOk(validate({foo:'aaab'}), 'not as')209t.ok(validate({foo:'a'}), 'as')210t.ok(validate({foo:'aaaaaa'}), 'as')211t.end()212})213214tape('custom format function', function(t) {215var validate = validator({216type: 'object',217properties: {218foo: {219type: 'string',220format: 'as'221}222}223}, {formats: {as:function(s) { return /^a+$/.test(s) } }})224225t.notOk(validate({foo:''}), 'not as')226t.notOk(validate({foo:'b'}), 'not as')227t.notOk(validate({foo:'aaab'}), 'not as')228t.ok(validate({foo:'a'}), 'as')229t.ok(validate({foo:'aaaaaa'}), 'as')230t.end()231})232233tape('do not mutate schema', function(t) {234var sch = {235items: [236{}237],238additionalItems: {239type: 'integer'240}241}242243var copy = JSON.parse(JSON.stringify(sch))244245validator(sch)246247t.same(sch, copy, 'did not mutate')248t.end()249})250251tape('#toJSON()', function(t) {252var schema = {253required: true,254type: 'object',255properties: {256hello: {type:'string', required:true}257}258}259260var validate = validator(schema)261262t.deepEqual(validate.toJSON(), schema, 'should return original schema')263t.end()264})265266tape('external schemas', function(t) {267var ext = {type: 'string'}268var schema = {269required: true,270$ref: '#ext'271}272273var validate = validator(schema, {schemas: {ext:ext}})274275t.ok(validate('hello string'), 'is a string')276t.notOk(validate(42), 'not a string')277t.end()278})279280tape('top-level external schema', function(t) {281var defs = {282"string": {283type: "string"284},285"sex": {286type: "string",287enum: ["male", "female", "other"]288}289}290var schema = {291type: "object",292properties: {293"name": { $ref: "definitions.json#/string" },294"sex": { $ref: "definitions.json#/sex" }295},296required: ["name", "sex"]297}298299var validate = validator(schema, {300schemas: {301"definitions.json": defs302}303})304t.ok(validate({name:"alice", sex:"female"}), 'is an object')305t.notOk(validate({name:"alice", sex: "bob"}), 'recognizes external schema')306t.notOk(validate({name:2, sex: "female"}), 'recognizes external schema')307t.end()308})309310tape('nested required array decl', function(t) {311var schema = {312properties: {313x: {314type: 'object',315properties: {316y: {317type: 'object',318properties: {319z: {320type: 'string'321}322},323required: ['z']324}325}326}327},328required: ['x']329}330331var validate = validator(schema)332333t.ok(validate({x: {}}), 'should be valid')334t.notOk(validate({}), 'should not be valid')335t.strictEqual(validate.errors[0].field, 'data.x', 'should output the missing field')336t.end()337})338339tape('verbose mode', function(t) {340var schema = {341required: true,342type: 'object',343properties: {344hello: {345required: true,346type: 'string'347}348}349};350351var validate = validator(schema, {verbose: true})352353t.ok(validate({hello: 'string'}), 'should be valid')354t.notOk(validate({hello: 100}), 'should not be valid')355t.strictEqual(validate.errors[0].value, 100, 'error object should contain the invalid value')356t.end()357})358359tape('additional props in verbose mode', function(t) {360var schema = {361type: 'object',362required: true,363additionalProperties: false,364properties: {365foo: {366type: 'string'367},368'hello world': {369type: 'object',370required: true,371additionalProperties: false,372properties: {373foo: {374type: 'string'375}376}377}378}379};380381var validate = validator(schema, {verbose: true})382383validate({'hello world': {bar: 'string'}});384385t.strictEqual(validate.errors[0].value, 'data["hello world"].bar', 'should output the path to the additional prop in the error')386t.end()387})388389tape('Date.now() is an integer', function(t) {390var schema = {type: 'integer'}391var validate = validator(schema)392393t.ok(validate(Date.now()), 'is integer')394t.end()395})396397tape('field shows item index in arrays', function(t) {398var schema = {399type: 'array',400items: {401type: 'array',402items: {403properties: {404foo: {405type: 'string',406required: true407}408}409}410}411}412413var validate = validator(schema)414415validate([416[417{ foo: 'test' },418{ foo: 'test' }419],420[421{ foo: 'test' },422{ baz: 'test' }423]424])425426t.strictEqual(validate.errors[0].field, 'data.1.1.foo', 'should output the field with specific index of failing item in the error')427t.end()428})429430431