UNPKG

@e22m4u/js-repository

Version:

Реализация репозитория для работы с базами данных в Node.js

1,554 lines (1,465 loc) 91.6 kB
import {expect} from 'chai'; import {format} from '@e22m4u/js-format'; import {DataType} from './properties/index.js'; import {DatabaseSchema} from '../../database-schema.js'; import {EmptyValuesService} from '@e22m4u/js-empty-values'; import {ModelDataValidator} from './model-data-validator.js'; import {DefinitionRegistry} from '../definition-registry.js'; import {PropertyValidatorRegistry} from './properties/index.js'; describe('ModelDataValidator', function () { describe('validate', function () { it('does not throw an error if a model does not have a property of a given data', function () { const dbs = new DatabaseSchema(); dbs.defineModel({name: 'model'}); dbs.getService(ModelDataValidator).validate('model', {foo: 'bar'}); }); it('throws an error if a given data is not a pure object', function () { const throwable = modelData => () => { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', }); dbs.getService(ModelDataValidator).validate('model', modelData); }; const error = v => format( 'The data of the model "model" should be an Object, but %s given.', v, ); expect(throwable('str')).to.throw(error('"str"')); expect(throwable(10)).to.throw(error('10')); expect(throwable(true)).to.throw(error('true')); expect(throwable(false)).to.throw(error('false')); expect(throwable([])).to.throw(error('Array')); expect(throwable(null)).to.throw(error('null')); expect(throwable(undefined)).to.throw(error('undefined')); }); it('uses a base model hierarchy to validate a given data', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'modelA', properties: { foo: DataType.STRING, }, }); dbs.defineModel({ name: 'modelB', base: 'modelA', }); const throwable = () => dbs.getService(ModelDataValidator).validate('modelB', {foo: 10}); expect(throwable).to.throw( 'The property "foo" of the model "modelB" must ' + 'have a String, but Number given.', ); }); it('throws an error if a given data does not have a required property', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', properties: { foo: { type: DataType.STRING, required: true, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', {}); expect(throwable).to.throw( 'The property "foo" of the model "model" ' + 'is required, but undefined given.', ); }); it('throws an error if a required property is undefined', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', properties: { foo: { type: DataType.STRING, required: true, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', {foo: undefined}); expect(throwable).to.throw( 'The property "foo" of the model "model" is required, but undefined given.', ); }); it('throws an error if a required property is null', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', properties: { foo: { type: DataType.STRING, required: true, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', {foo: null}); expect(throwable).to.throw( 'The property "foo" of the model "model" is required, but null given.', ); }); it('throws an error if a required property has an empty value', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', properties: { foo: { type: DataType.STRING, required: true, }, }, }); dbs .getService(EmptyValuesService) .setEmptyValuesOf(DataType.STRING, ['empty']); const throwable = () => dbs.getService(ModelDataValidator).validate('model', {foo: 'empty'}); expect(throwable).to.throw( 'The property "foo" of the model "model" ' + 'is required, but "empty" given.', ); }); describe('an option "isPartial" is true', function () { it('does not throw an error if a given data does not have a required property', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', properties: { foo: { type: DataType.STRING, required: true, }, }, }); dbs.getService(ModelDataValidator).validate('model', {}, true); }); it('throws an error if a required property is undefined', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', properties: { foo: { type: DataType.STRING, required: true, }, }, }); const throwable = () => dbs .getService(ModelDataValidator) .validate('model', {foo: undefined}, true); expect(throwable).to.throw( 'The property "foo" of the model "model" ' + 'is required, but undefined given.', ); }); it('throws an error if a required property is null', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', properties: { foo: { type: DataType.STRING, required: true, }, }, }); const throwable = () => dbs .getService(ModelDataValidator) .validate('model', {foo: null}, true); expect(throwable).to.throw( 'The property "foo" of the model "model" is required, but null given.', ); }); it('throws an error if a required property has an empty value', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', properties: { foo: { type: DataType.STRING, required: true, }, }, }); dbs .getService(EmptyValuesService) .setEmptyValuesOf(DataType.STRING, [5]); const throwable = () => dbs.getService(ModelDataValidator).validate('model', {foo: 5}, true); expect(throwable).to.throw( 'The property "foo" of the model "model" is required, but 5 given.', ); }); }); describe('validate by property type', function () { it('skips validation for an empty value', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.STRING, }, }, }); dbs .getService(EmptyValuesService) .setEmptyValuesOf(DataType.STRING, [5]); dbs.getService(ModelDataValidator).validate('model', {foo: 5}); }); describe('DataType.ANY', function () { describe('ShortPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ANY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ANY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('does not throw an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ANY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); }); it('does not throw an error if a number given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ANY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: 10, }); }); it('does not throw an error if true given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ANY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: true, }); }); it('does not throw an error if false given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ANY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: false, }); }); it('does not throw an error if an array given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ANY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: [], }); }); it('does not throw an error if an object given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ANY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: {}, }); }); }); describe('FullPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ANY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ANY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('does not throw an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ANY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); }); it('does not throw an error if a number given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ANY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: 10, }); }); it('does not throw an error if true given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ANY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: true, }); }); it('does not throw an error if false given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ANY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: false, }); }); it('does not throw an error if an array given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ANY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: [], }); }); it('does not throw an error if an object given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ANY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: {}, }); }); }); }); describe('DataType.STRING', function () { describe('ShortPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.STRING, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.STRING, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('does not throw an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.STRING, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); }); it('throws an error if a number given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.STRING, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 10, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Number given.', ); }); it('throws an error if true given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.STRING, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: true, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Boolean given.', ); }); it('throws an error if false given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.STRING, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: false, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Boolean given.', ); }); it('throws an error if an array given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.STRING, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: [], }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Array given.', ); }); it('throws an error if an object given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.STRING, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: {}, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Object given.', ); }); }); describe('FullPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.STRING, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.STRING, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('does not throw an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.STRING, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); }); it('throws an error if a number given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.STRING, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 10, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Number given.', ); }); it('throws an error if true given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.STRING, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: true, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Boolean given.', ); }); it('throws an error if false given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.STRING, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: false, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Boolean given.', ); }); it('throws an error if an array given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.STRING, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: [], }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Array given.', ); }); it('throws an error if an object given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.STRING, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: {}, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a String, but Object given.', ); }); }); }); describe('DataType.NUMBER', function () { describe('ShortPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.NUMBER, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.NUMBER, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('throws an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.NUMBER, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but String given.', ); }); it('does not throw an error if a number given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.NUMBER, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: 10, }); }); it('throws an error if true given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.NUMBER, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: true, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but Boolean given.', ); }); it('throws an error if false given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.NUMBER, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: false, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but Boolean given.', ); }); it('throws an error if an array given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.NUMBER, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: [], }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but Array given.', ); }); it('throws an error if an object given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.NUMBER, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: {}, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but Object given.', ); }); }); describe('FullPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.NUMBER, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.NUMBER, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('throws an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.NUMBER, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but String given.', ); }); it('does not throw an error if a number given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.NUMBER, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: 10, }); }); it('throws an error if true given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.NUMBER, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: true, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but Boolean given.', ); }); it('throws an error if false given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.NUMBER, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: false, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but Boolean given.', ); }); it('throws an error if an array given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.NUMBER, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: [], }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but Array given.', ); }); it('throws an error if an object given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.NUMBER, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: {}, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Number, but Object given.', ); }); }); }); describe('DataType.BOOLEAN', function () { describe('ShortPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.BOOLEAN, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.BOOLEAN, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('throws an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.BOOLEAN, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Boolean, but String given.', ); }); it('throws an error if a number given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.BOOLEAN, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 10, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Boolean, but Number given.', ); }); it('does not throw an error if true given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.BOOLEAN, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: true, }); }); it('does not throw an error if false given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.BOOLEAN, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: false, }); }); it('throws an error if an array given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.BOOLEAN, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: [], }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Boolean, but Array given.', ); }); it('throws an error if an object given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.BOOLEAN, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: {}, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Boolean, but Object given.', ); }); }); describe('FullPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.BOOLEAN, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.BOOLEAN, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('throws an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.BOOLEAN, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Boolean, but String given.', ); }); it('throws an error if a number given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.BOOLEAN, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 10, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Boolean, but Number given.', ); }); it('does not throw an error if true given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.BOOLEAN, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: true, }); }); it('does not throw an error if false given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.BOOLEAN, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: false, }); }); it('throws an error if an array given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.BOOLEAN, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: [], }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Boolean, but Array given.', ); }); it('throws an error if an object given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.BOOLEAN, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: {}, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'a Boolean, but Object given.', ); }); }); }); describe('DataType.ARRAY', function () { describe('ShortPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ARRAY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ARRAY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('throws an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ARRAY, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'an Array, but String given.', ); }); it('throws an error if a number given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ARRAY, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 10, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'an Array, but Number given.', ); }); it('throws an error if true given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ARRAY, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: true, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'an Array, but Boolean given.', ); }); it('throws an error if false given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ARRAY, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: false, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'an Array, but Boolean given.', ); }); it('does not throw an error if an array given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ARRAY, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: [], }); }); it('throws an error if an object given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: DataType.ARRAY, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: {}, }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'an Array, but Object given.', ); }); }); describe('FullPropertyDefinition', function () { it('does not throw an error if an undefined given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ARRAY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: undefined, }); }); it('does not throw an error if a null given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ARRAY, }, }, }); dbs.getService(ModelDataValidator).validate('model', { foo: null, }); }); it('throws an error if a string given', function () { const dbs = new DatabaseSchema(); dbs.defineModel({ name: 'model', datasource: 'datasource', properties: { foo: { type: DataType.ARRAY, }, }, }); const throwable = () => dbs.getService(ModelDataValidator).validate('model', { foo: 'bar', }); expect(throwable).to.throw( 'The property "foo" of the model "model" must have ' + 'an Array, but String given.', ); }); it('throws an error if a number given', function () { const dbs = new DatabaseSchema();