Skip to content

Commit

Permalink
test(definitions): TestHelper.validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed May 28, 2024
1 parent 224f08a commit 0febb6b
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 467 deletions.
3 changes: 3 additions & 0 deletions test/ajv-schemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,8 @@ function schemesV3_0_0(ajv) {
ajv.addSchema(require('@common/avroSchema_v1.json'));
ajv.addSchema(require('@common/openapiSchema_3_0.json'));

ajv.addSchema(require('@extensions/x/0.1.0/schema.json'));
ajv.addSchema(require('@extensions/linkedin/0.1.0/schema.json'));

return bindingSchemes(ajv);
}
37 changes: 12 additions & 25 deletions test/definitions/3.0.0/models/channel/channel/channel.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,51 @@
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);
import TestHelper from '@test/test-helper';

const jsonSchemaName = 'Channel';
const jsonSchema = require('@definitions/3.0.0/channel.json');
import schemesV3_0_0 from '@test/ajv-schemes';
const validator = schemesV3_0_0(ajv).compile(jsonSchema);
const fs = require('fs');
const assert = require('assert');
const title = 'Channel'
const validator = TestHelper.validator(require('@definitions/3.0.0/channel.json'))

describe.skip(`${jsonSchemaName}. Reason: multiple errors with bindings, externalDoc, ...`, () => {
describe.skip(`${title}. Reason: multiple errors with bindings, externalDoc, ...`, () => {
it('example', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/example.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, `${jsonSchemaName} example MUST be valid`);
assert(validationResult === true, `${title} example MUST be valid`);
});

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

assert(validationResult === true, `${jsonSchemaName} with empty body is valid`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} without required properties is valid`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} is valid with only required properties`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} can be extended`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} is not valid when was wrongly extended`);
assert(validationResult === false, `${title} 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
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);
import TestHelper from '@test/test-helper';

const jsonSchemaName = 'Correlation ID';
const jsonSchema = require('@definitions/3.0.0/correlationId.json');
import schemesV3_0_0 from '@test/ajv-schemes';
const validator = schemesV3_0_0(ajv).compile(jsonSchema);
const fs = require('fs');
const assert = require('assert');
const title = 'Correlation ID'
const validator = TestHelper.validator(require('@definitions/3.0.0/correlationId.json'))

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

assert(validationResult === true, `${jsonSchemaName} example MUST be valid`);
assert(validationResult === true, `${title} example MUST be valid`);
});

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

assert(validationResult === false, `${jsonSchemaName} with empty body is valid`);
assert(validationResult === false, `${title} with empty body is valid`);
assert(validator.errors[0].message === 'must have required property \'location\'');
assert(validator.errors.length === 1);
});
Expand All @@ -39,7 +26,7 @@ describe(`${jsonSchemaName}`, () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/without required properties.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, `${jsonSchemaName} without required properties is valid`);
assert(validationResult === false, `${title} without required properties is valid`);
assert(validator.errors[0].message === 'must have required property \'location\'');
assert(validator.errors.length === 1);
});
Expand All @@ -48,21 +35,21 @@ describe(`${jsonSchemaName}`, () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/only required properties.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, `${jsonSchemaName} is valid with only required properties`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} can be extended`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} is not valid when was wrongly extended`);
assert(validationResult === false, `${title} 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);
Expand Down
37 changes: 12 additions & 25 deletions test/definitions/3.0.0/models/channel/message/message/message.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,51 @@
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);
import TestHelper from '@test/test-helper';

const jsonSchemaName = 'Message';
const jsonSchema = require('@definitions/3.0.0/messageObject.json');
import schemesV3_0_0 from '@test/ajv-schemes';
const validator = schemesV3_0_0(ajv).compile(jsonSchema);
const fs = require('fs');
const assert = require('assert');
const title = 'Message'
const validator = TestHelper.validator(require('@definitions/3.0.0/messageObject.json'));

describe.skip(`${jsonSchemaName}. Issues with bindings`, () => {
describe.skip(`${title}. Issues with bindings`, () => {
it('example', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/example.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, `${jsonSchemaName} example MUST be valid`);
assert(validationResult === true, `${title} example MUST be valid`);
});

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

assert(validationResult === true, `${jsonSchemaName} with empty body is valid`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} without required properties is valid`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} is valid with only required properties`);
assert(validationResult === true, `${title} is valid with only required properties`);
});

it.skip('extended. Reason: schema prohibites extendsion. Check', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/extended.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, `${jsonSchemaName} can be extended`);
assert(validationResult === true, `${title} can be extended`);
});

it.skip('wrongly extended. Reason: schema prohibites extendsion. Check', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/wrongly extended.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, `${jsonSchemaName} is not valid when was wrongly extended`);
assert(validationResult === false, `${title} 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
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);
import TestHelper from '@test/test-helper';

const jsonSchemaName = 'Message example';
const jsonSchema = require('@definitions/3.0.0/messageExampleObject.json');
import schemesV3_0_0 from '@test/ajv-schemes';
const validator = schemesV3_0_0(ajv).compile(jsonSchema);
const fs = require('fs');
const assert = require('assert');
const title = 'Message example'
const validator = TestHelper.validator(require('@definitions/3.0.0/messageExampleObject.json'));

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

assert(validationResult === true, `${jsonSchemaName} example MUST be valid`);
assert(validationResult === true, `${title} example MUST be valid`);
});

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

assert(validationResult === false, `${jsonSchemaName} with empty body is valid`);
assert(validationResult === false, `${title} with empty body is valid`);
assert(validator.errors[0].message === 'must have required property \'payload\'');
assert(validator.errors[1].message === 'must have required property \'headers\'');
assert(validator.errors[2].message === 'must match a schema in anyOf');
Expand All @@ -41,7 +28,7 @@ describe(`${jsonSchemaName}`, () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/without required properties.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, `${jsonSchemaName} without required properties is valid`);
assert(validationResult === false, `${title} without required properties is valid`);
assert(validator.errors[0].message === 'must have required property \'payload\'');
assert(validator.errors[1].message === 'must have required property \'headers\'');
assert(validator.errors[2].message === 'must match a schema in anyOf');
Expand All @@ -52,21 +39,21 @@ describe(`${jsonSchemaName}`, () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/only required properties.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, `${jsonSchemaName} is valid with only required properties`);
assert(validationResult === true, `${title} is valid with only required properties`);
});

it.skip('extended. Reason: schema prohibites extendsion. Check', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/extended.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, `${jsonSchemaName} can be extended`);
assert(validationResult === true, `${title} can be extended`);
});

it.skip('wrongly extended. Reason: schema prohibites extendsion. Check', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/wrongly extended.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, `${jsonSchemaName} is not valid when was wrongly extended`);
assert(validationResult === false, `${title} 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,51 @@
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);
import TestHelper from '@test/test-helper';

const jsonSchemaName = 'Message trait';
const jsonSchema = require('@definitions/3.0.0/messageTrait.json');
import schemesV3_0_0 from '@test/ajv-schemes';
const validator = schemesV3_0_0(ajv).compile(jsonSchema);
const fs = require('fs');
const assert = require('assert');
const title = 'Message trait'
const validator = TestHelper.validator(require('@definitions/3.0.0/messageTrait.json'));

describe.skip(`${jsonSchemaName}. Issues with bindings`, () => {
describe.skip(`${title}. Issues with bindings`, () => {
it('example', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/example.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, `${jsonSchemaName} example MUST be valid`);
assert(validationResult === true, `${title} example MUST be valid`);
});

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

assert(validationResult === true, `${jsonSchemaName} with empty body is valid`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} without required properties is valid`);
assert(validationResult === true, `${title} 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, `${jsonSchemaName} is valid with only required properties`);
assert(validationResult === true, `${title} is valid with only required properties`);
});

it.skip('extended. Reason: schema prohibites extendsion. Check', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/extended.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === true, `${jsonSchemaName} can be extended`);
assert(validationResult === true, `${title} can be extended`);
});

it.skip('wrongly extended. Reason: schema prohibites extendsion. Check', () => {
const info = JSON.parse(fs.readFileSync(`${__dirname}/wrongly extended.json`, 'utf-8'));
const validationResult = validator(info);

assert(validationResult === false, `${jsonSchemaName} is not valid when was wrongly extended`);
assert(validationResult === false, `${title} 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);
Expand Down
Loading

0 comments on commit 0febb6b

Please sign in to comment.