Skip to content

Commit

Permalink
feat: increased code coverage (#37)
Browse files Browse the repository at this point in the history
* feat: increased code coverage

* feat: increased code coverage

* feat: increased code coverage
  • Loading branch information
muridot0 authored Dec 31, 2023
1 parent 9450f3a commit 6b6da81
Show file tree
Hide file tree
Showing 2 changed files with 275 additions and 2 deletions.
275 changes: 275 additions & 0 deletions base/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { Schema, SchemaDefinitions, findRelations } from './schema.js'
import { describe, expect, it, test } from 'vitest'

describe('schema', () => {
it('throws an error when data is null', () => {
const schema = new Schema({})
expect(() => schema.validate(null)).toThrow('`data` is undefined')
})

describe('string type', () => {
const schema = new Schema({
fullname: { type: 'string' },
Expand Down Expand Up @@ -350,6 +355,72 @@ describe('schema', () => {
expect(schema.validate({})).toStrictEqual({})
})

it('throws an error when getRef is not defined', () => {
const schema = {
address: {
schema: 'string',
type: 'object',
},
}

expect(() =>
new Schema({
address: {
schema: 'string',
type: 'object',
},
}).validate(schema)
).toThrow('`getRef` is required when schema is a string.')
})

it('throws an error when schema cannot be found', () => {
const schema = {
address: {
schema: 'string',
type: 'object',
},
}

expect(() =>
new Schema(
{
address: {
schema: 'string',
type: 'object',
},
},
{
getRef: () => {
return undefined
},
}
).validate(schema)
).toThrow('Schema ref with name `string` not found')
})

it('returns a valid schema definition when it exists', () => {
expect(
new Schema(
{
address: {
schema: 'address',
type: 'object',
},
},
{
getRef: (name) => {
if (name !== 'address') {
return
}
return {
line1: { type: 'string' },
}
},
}
).validate({ address: { line1: 'Nii Sai' } })
).toStrictEqual({ address: { line1: 'Nii Sai' } })
})

describe('when required', () => {
const schema = new Schema({
profile: {
Expand Down Expand Up @@ -902,6 +973,7 @@ describe('schema', () => {
it('validates', () => {
const schema: SchemaDefinitions = {
address: {
defaultValue: { line1: 'chale' },
schema: {
line1: { required: true, type: 'string' },
line2: { type: 'string' },
Expand All @@ -925,6 +997,209 @@ describe('schema', () => {

expect(() => Schema.validateSchema(schema)).not.toThrow()
})

it('throws a validation error when type is unknown', () => {
const schema = {
stuffs: { type: '' },
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`type` is invalid or undefined')
})

describe('object type', () => {
it('throws an error when schema definitions is not an object', () => {
const schema = [{ stuffs: { type: 'array' } }]

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('schema has to be an object')
})

it('throws a validation error when schema does not exist', () => {
const schema = {
stuff: {
defaultValue: true,
type: 'object',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`schema` is required when type is `object`')
})

it('throws a validation error when object schema defaultValue is not an object', () => {
const schema = {
stuff: {
defaultValue: 'man',
schema: {},
type: 'object',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`defaultValue` should be an object')
})

it('throws a validation error when object schema defaultValue is an array', () => {
const schema = {
stuff: {
defaultValue: ['man'],
schema: {},
type: 'object',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`defaultValue` should be an object')
})
})

describe('array type', () => {
it('throws a validation error when items is not defined for an array', () => {
const schema = {
stuffs: { type: 'array' },
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`items` is required when type is `array`')
})

it('throws a validation error when defaultValue is not an array', () => {
const schema = {
stuff: {
defaultValue: 'name',
items: { defaultValue: 'string', type: 'string' },
type: 'array',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`defaultValue` should be an array')
})

it('throws an error when defaultValue does not match items definition', () => {
const schema: SchemaDefinitions = {
stuff: {
defaultValue: [1, 2, 3, 4],
items: { defaultValue: 'string', type: 'string' },
type: 'array',
},
}

expect(() => Schema.validateSchema(schema)).toThrow(
'item: value is not of type `array`'
)
})
})

describe('boolean type', () => {
it('throws a validation error when default value is not boolean', () => {
const schema = {
stuff: {
defaultValue: 'true',
type: 'boolean',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`defaultValue` should be a boolean')
})
})

describe('date type', () => {
it('throws validation error when default value is not a date', () => {
const schema = {
stuff: {
defaultValue: 'true',
type: 'date',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`defaultValue` should be a valid date')
})
})

describe('id type', () => {
it('throws a validation error when default value is not a string', () => {
const schema = {
stuff: {
defaultValue: true,
type: 'id',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`defaultValue` should be a string')
})

it('requires a relation when type is id', () => {
const schema = {
stuff: {
defaultValue: '1234',
type: 'id',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`relation` is required for type `id`')
})
})

describe('number type', () => {
it('throws a validation error when defalult value is not a number', () => {
const schema = {
stuff: {
defaultValue: true,
type: 'number',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`defaultValue` should be a number')
})
})

describe('string type', () => {
it('throws a validation error when default value is not a string', () => {
const schema = {
stuff: {
defaultValue: {},
type: 'string',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).not.toThrow('`defaultValue` should be an string')
})

it('throws an validation error when enum has no value', () => {
const schema = {
stuff: {
defaultValue: 'string',
enum: [],
type: 'string',
},
}

expect(() =>
Schema.validateSchema(schema as unknown as SchemaDefinitions)
).toThrow('`enum` should have at least one value')
})
})
})
})

Expand Down
2 changes: 0 additions & 2 deletions base/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,7 @@ class Schema {
} catch (err) {
if (err instanceof ValidationError) {
err.field = `${fieldPath}.${index}.${err.field}`
throw err
}

throw err
}
}
Expand Down

0 comments on commit 6b6da81

Please sign in to comment.