From 4d4124a8bdf955c6a8e9acc62c04c955ae8e8bf3 Mon Sep 17 00:00:00 2001 From: Pavel Bodiachevskii Date: Tue, 21 May 2024 21:08:20 +0400 Subject: [PATCH] test(definitions): contact check Contact Json Schema https://github.com/asyncapi/spec-json-schemas/issues/539 --- test/definitions/3.0.0/contact/contact.js | 60 +++++++++++++++++++ test/definitions/3.0.0/contact/empty.json | 1 + test/definitions/3.0.0/contact/extended.json | 10 ++++ .../contact/only required properties.json | 1 + .../contact/without required properties.json | 1 + .../3.0.0/contact/wrongly extended.json | 11 ++++ test/index.js | 1 + 7 files changed, 85 insertions(+) create mode 100644 test/definitions/3.0.0/contact/contact.js create mode 100644 test/definitions/3.0.0/contact/empty.json create mode 100644 test/definitions/3.0.0/contact/extended.json create mode 100644 test/definitions/3.0.0/contact/only required properties.json create mode 100644 test/definitions/3.0.0/contact/without required properties.json create mode 100644 test/definitions/3.0.0/contact/wrongly extended.json diff --git a/test/definitions/3.0.0/contact/contact.js b/test/definitions/3.0.0/contact/contact.js new file mode 100644 index 00000000..13c37f01 --- /dev/null +++ b/test/definitions/3.0.0/contact/contact.js @@ -0,0 +1,60 @@ +const Ajv = require('ajv'); +const assert = require('assert'); +const addFormats = require('ajv-formats'); +const fs = require('fs'); + +const ajv = new Ajv({ + jsonPointers: true, + allErrors: true, + schemaId: '$id', + logger: false, + validateFormats: true, + strict: false, +}); +addFormats(ajv); + +const infoJsonSchema = require('../../../../definitions/3.0.0/contact.json'); +const validator = ajv + .addMetaSchema(require('../../../../definitions/3.0.0/schema.json')) + .addSchema(require('../../../../definitions/3.0.0/specificationExtension.json')) + .compile(infoJsonSchema); + +describe('Contact', () => { + it('empty', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, 'Contact with empty body is valid'); + }); + + it('without required properties', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/without required properties.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, 'Contact without required properties is valid'); + }); + + it('only required properties', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/only required properties.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, 'Contact is valid with only required properties'); + }); + + it('extended', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/extended.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === true, 'Contact can be extended'); + }); + + it('wrongly extended', () => { + const info = JSON.parse(fs.readFileSync(`${__dirname}/wrongly extended.json`, 'utf-8')); + const validationResult = validator(info); + + assert(validationResult === false, 'Contact is not valid when was wrongly extended'); + assert(validator.errors[0].message === 'must NOT have additional properties'); + assert(validator.errors[0].params.additionalProperty === 'ext-number'); + assert(validator.errors.length === 1); + }); +}); diff --git a/test/definitions/3.0.0/contact/empty.json b/test/definitions/3.0.0/contact/empty.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/test/definitions/3.0.0/contact/empty.json @@ -0,0 +1 @@ +{} diff --git a/test/definitions/3.0.0/contact/extended.json b/test/definitions/3.0.0/contact/extended.json new file mode 100644 index 00000000..fbc417ba --- /dev/null +++ b/test/definitions/3.0.0/contact/extended.json @@ -0,0 +1,10 @@ +{ + "name" : "AsyncAPI", + "url" : "https://www.asyncapi.com", + "email" : "java@asyncapi.com", + "x-number" : 0, + "x-string" : "", + "x-object" : { + "property" : { } + } +} diff --git a/test/definitions/3.0.0/contact/only required properties.json b/test/definitions/3.0.0/contact/only required properties.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/test/definitions/3.0.0/contact/only required properties.json @@ -0,0 +1 @@ +{} diff --git a/test/definitions/3.0.0/contact/without required properties.json b/test/definitions/3.0.0/contact/without required properties.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/test/definitions/3.0.0/contact/without required properties.json @@ -0,0 +1 @@ +{} diff --git a/test/definitions/3.0.0/contact/wrongly extended.json b/test/definitions/3.0.0/contact/wrongly extended.json new file mode 100644 index 00000000..fe3dcbfc --- /dev/null +++ b/test/definitions/3.0.0/contact/wrongly extended.json @@ -0,0 +1,11 @@ +{ + "name" : "AsyncAPI", + "url" : "https://www.asyncapi.com", + "email" : "java@asyncapi.com", + "x-number" : 0, + "x-string" : "", + "x-object" : { + "property" : { } + }, + "ext-number": 1 +} diff --git a/test/index.js b/test/index.js index e3a60ae5..e8fc7a42 100644 --- a/test/index.js +++ b/test/index.js @@ -4,6 +4,7 @@ const path = require('path'); describe('AsyncAPI: 3.0.0', () => { require('./definitions/3.0.0/info/info.js'); + require('./definitions/3.0.0/contact/contact.js') }); describe('AsyncAPI', () => {