-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
567d2a3
commit c1fdff2
Showing
8 changed files
with
5,134 additions
and
3,008 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module.exports = { | ||
transform: { | ||
'^.+\\.ts$': ['ts-jest', { tsconfig: 'tsconfig.json' }], | ||
}, | ||
coverageReporters: ['text', 'html'], | ||
collectCoverage: true, | ||
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!*/node_modules/', '!/vendor/**', '!*/common/**', '!**/models/**', '!<rootDir>/src/*'], | ||
coverageDirectory: '<rootDir>/coverage', | ||
rootDir: '../../.', | ||
testMatch: ['<rootDir>/tests/**/*.spec.ts'], | ||
// setupFiles: ['<rootDir>/tests/configurations/jest.setup.ts'], | ||
// setupFilesAfterEnv: ['jest-openapi', '<rootDir>/tests/configurations/initJestOpenapi.setup.ts'], | ||
// reporters: [ | ||
// 'default', | ||
// [ | ||
// 'jest-html-reporters', | ||
// { multipleReportsUnitePath: './reports', pageTitle: 'integration', publicPath: './reports', filename: 'integration.html' }, | ||
// ], | ||
// ], | ||
moduleDirectories: ['node_modules', 'src'], | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
coverageThreshold: { | ||
global: { | ||
branches: 80, | ||
functions: 80, | ||
lines: 80, | ||
statements: -10, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { JSONSchema } from '@apidevtools/json-schema-ref-parser'; | ||
import { loadSchema } from '../src/schemas'; | ||
|
||
describe('schemas', () => { | ||
describe('#loadSchema', () => { | ||
it('should load the schema', async () => { | ||
const schema = { | ||
$id: 'https://mapcolonies.com/test', | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'string' }, | ||
}, | ||
required: ['foo'], | ||
} satisfies JSONSchema; | ||
const dereferencedSchema = await loadSchema(schema); | ||
expect(dereferencedSchema).toEqual(schema); | ||
}); | ||
|
||
it('should load the schema with a reference', async () => { | ||
const schema = { | ||
$id: 'https://mapcolonies.com/common/db/full/v1', | ||
description: 'The full database schema including schema and database name', | ||
type: 'object', | ||
allOf: [ | ||
{ | ||
$ref: 'https://mapcolonies.com/common/db/partial/v1', | ||
}, | ||
{ | ||
$ref: '#/definitions/db', | ||
}, | ||
], | ||
definitions: { | ||
db: { | ||
type: 'object', | ||
required: ['database'], | ||
properties: { | ||
schema: { | ||
type: 'string', | ||
description: 'The schema name of the database', | ||
default: 'public', | ||
}, | ||
database: { | ||
type: 'string', | ||
description: 'The database name', | ||
maxLength: 63, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} satisfies JSONSchema; | ||
|
||
const dereferencedSchema = await loadSchema(schema); | ||
|
||
expect(dereferencedSchema).toEqual({ | ||
$id: 'https://mapcolonies.com/test', | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'string' }, | ||
}, | ||
required: ['foo'], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { validate, ajvOptionsValidator, ajvConfigValidator } from '../src/validator'; | ||
|
||
describe('validator', () => { | ||
describe('#validate', () => { | ||
it('should return the validated data if the data is valid', () => { | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'string' }, | ||
}, | ||
required: ['foo'], | ||
}; | ||
const data = { foo: 'bar' }; | ||
const [errors, validatedData] = validate(ajvOptionsValidator, schema, data); | ||
expect(errors).toBeUndefined(); | ||
expect(validatedData).toEqual(data); | ||
}); | ||
|
||
it('should return the errors if the data is invalid', () => { | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'string' }, | ||
}, | ||
required: ['foo'], | ||
}; | ||
const data = {}; | ||
const [errors, validatedData] = validate(ajvOptionsValidator, schema, data); | ||
expect(errors).toBeDefined(); | ||
expect(validatedData).toBeUndefined(); | ||
}); | ||
|
||
it('should coerce types and insert defaults if options validator is used', () => { | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'number' }, | ||
bar: { type: 'string', default: 'baz' }, | ||
}, | ||
required: ['foo'], | ||
}; | ||
const data = { foo: '1' }; | ||
const [errors, validatedData] = validate(ajvOptionsValidator, schema, data); | ||
expect(errors).toBeUndefined(); | ||
expect(validatedData).toEqual({ foo: 1, bar: 'baz' }); | ||
}); | ||
|
||
it('should insert defaults and allow x-env-value keyword if config validator is used', () => { | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'number', default: 1 }, | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
bar: { type: 'string', 'x-env-value': 'BAR' }, | ||
}, | ||
required: ['foo'], | ||
}; | ||
const data = {}; | ||
const [errors, validatedData] = validate(ajvConfigValidator, schema, data); | ||
expect(errors).toBeUndefined(); | ||
expect(validatedData).toEqual({ foo: 1 }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters