Skip to content

Commit

Permalink
test(definitions): reference object
Browse files Browse the repository at this point in the history
check ReferenceObject Json Schema

asyncapi#539
  • Loading branch information
Pakisan committed May 21, 2024
1 parent e6a89a4 commit e5726e9
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test/definitions/3.0.0/reference object/array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$ref": [
1,
null,
false,
"#/components/schemas/user",
{
"$ref": "#/components/schemas/user"
}
]
}
3 changes: 3 additions & 0 deletions test/definitions/3.0.0/reference object/boolean.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$ref": true
}
3 changes: 3 additions & 0 deletions test/definitions/3.0.0/reference object/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$ref": ""
}
3 changes: 3 additions & 0 deletions test/definitions/3.0.0/reference object/null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$ref": null
}
3 changes: 3 additions & 0 deletions test/definitions/3.0.0/reference object/number.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$ref": 1234
}
5 changes: 5 additions & 0 deletions test/definitions/3.0.0/reference object/object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$ref": {
"$ref": "#/components/schemas/user"
}
}
90 changes: 90 additions & 0 deletions test/definitions/3.0.0/reference object/reference-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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/Reference.json');
const validator = ajv
.addMetaSchema(require('../../../../definitions/3.0.0/schema.json'))
.addMetaSchema(require('../../../../definitions/3.0.0/ReferenceObject.json'))
.compile(infoJsonSchema);

describe('ReferenceObject', () => {
it('empty', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, 'Reference with empty $ref is valid');
});

it('number', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/number.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, 'Reference as number is not valid');
assert(validator.errors[0].message === 'must be string');
assert(validator.errors.length === 1);
});

it('object', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/object.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, 'Reference as object is not valid');
assert(validator.errors[0].message === 'must be string');
assert(validator.errors.length === 1);
});

it('string', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/string.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, 'Reference as non URI is not valid');
assert(validator.errors[0].message === 'must match format "uri-reference"');
assert(validator.errors.length === 1);
});

it('boolean', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/boolean.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, 'Reference as boolean is not valid');
assert(validator.errors[0].message === 'must be string');
assert(validator.errors.length === 1);
});

it('null', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/null.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, 'Reference as null is not valid');
assert(validator.errors[0].message === 'must be string');
assert(validator.errors.length === 1);
});

it('array', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/array.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, 'Reference as array is not valid');
assert(validator.errors[0].message === 'must be string');
assert(validator.errors.length === 1);
});

it('URI', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/uri.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, 'Reference in URI format is valid');
});
});
3 changes: 3 additions & 0 deletions test/definitions/3.0.0/reference object/string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$ref": "string value"
}
3 changes: 3 additions & 0 deletions test/definitions/3.0.0/reference object/uri.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$ref": "#/components/schemas/user"
}
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('AsyncAPI: 3.0.0', () => {
require('./definitions/3.0.0/contact/contact.js');
require('./definitions/3.0.0/license/license.js');
require('./definitions/3.0.0/reference/reference.js');
require('./definitions/3.0.0/reference object/reference-object.js');
});

describe('AsyncAPI', () => {
Expand Down

0 comments on commit e5726e9

Please sign in to comment.