@e22m4u/js-repository 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -33
- package/assets/mermaid-diagram.png +0 -0
- package/assets/mermaid-diagram.txt +29 -0
- package/docs/assets/main.js +4 -4
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/Adapter.html +14 -14
- package/docs/classes/AdapterLoader.html +2 -2
- package/docs/classes/AdapterRegistry.html +2 -2
- package/docs/classes/BelongsToResolver.html +3 -3
- package/docs/classes/DatasourceDefinitionValidator.html +2 -2
- package/docs/classes/DefinitionRegistry.html +7 -7
- package/docs/classes/FieldsClauseTool.html +4 -4
- package/docs/classes/HasManyResolver.html +4 -4
- package/docs/classes/HasOneResolver.html +4 -4
- package/docs/classes/IncludeClauseTool.html +6 -6
- package/docs/classes/InvalidArgumentError.html +1 -1
- package/docs/classes/InvalidOperatorValueError.html +2 -2
- package/docs/classes/ModelDataSanitizer.html +2 -2
- package/docs/classes/ModelDataValidator.html +3 -5
- package/docs/classes/ModelDefinitionUtils.html +17 -17
- package/docs/classes/ModelDefinitionValidator.html +2 -2
- package/docs/classes/NotImplementedError.html +1 -1
- package/docs/classes/OperatorClauseTool.html +14 -14
- package/docs/classes/OrderClauseTool.html +4 -4
- package/docs/classes/PrimaryKeysDefinitionValidator.html +2 -2
- package/docs/classes/PropertiesDefinitionValidator.html +2 -2
- package/docs/classes/PropertyValidatorRegistry.html +20 -0
- package/docs/classes/ReferencesManyResolver.html +2 -2
- package/docs/classes/RelationsDefinitionValidator.html +2 -2
- package/docs/classes/Repository.html +17 -17
- package/docs/classes/RepositoryRegistry.html +3 -3
- package/docs/classes/Schema.html +4 -4
- package/docs/classes/SliceClauseTool.html +4 -4
- package/docs/classes/WhereClauseTool.html +3 -3
- package/docs/enums/DataType.html +2 -2
- package/docs/enums/DecoratorTargetType.html +2 -2
- package/docs/enums/RelationType.html +2 -2
- package/docs/functions/capitalize.html +1 -1
- package/docs/functions/cloneDeep.html +1 -1
- package/docs/functions/excludeObjectKeys.html +1 -1
- package/docs/functions/getCtorName.html +1 -1
- package/docs/functions/getDecoratorTargetType.html +1 -1
- package/docs/functions/getValueByPath.html +1 -1
- package/docs/functions/isCtor.html +1 -1
- package/docs/functions/isPureObject.html +1 -1
- package/docs/functions/selectObjectKeys.html +1 -1
- package/docs/functions/singularize.html +1 -1
- package/docs/functions/stringToRegexp.html +1 -1
- package/docs/index.html +18 -4
- package/docs/interfaces/AndClause.html +2 -2
- package/docs/interfaces/Constructor.html +2 -2
- package/docs/interfaces/OrClause.html +2 -2
- package/docs/modules.html +4 -0
- package/docs/types/AnyObject.html +1 -1
- package/docs/types/BelongsToDefinition.html +1 -1
- package/docs/types/DEFAULT_PRIMARY_KEY_PROPERTY_NAME.html +1 -1
- package/docs/types/DatasourceDefinition.html +1 -1
- package/docs/types/FieldsClause.html +1 -1
- package/docs/types/FilterClause.html +1 -1
- package/docs/types/Flatten.html +1 -1
- package/docs/types/FullPropertyDefinition.html +2 -2
- package/docs/types/HasManyDefinition.html +1 -1
- package/docs/types/HasOneDefinition.html +1 -1
- package/docs/types/Identity.html +1 -1
- package/docs/types/IncludeClause.html +1 -1
- package/docs/types/ItemFilterClause.html +1 -1
- package/docs/types/ModelData.html +1 -1
- package/docs/types/ModelDefinition.html +1 -1
- package/docs/types/ModelId.html +1 -1
- package/docs/types/NestedIncludeClause.html +1 -1
- package/docs/types/NormalizedFieldsClause.html +1 -1
- package/docs/types/NormalizedIncludeClause.html +1 -1
- package/docs/types/OperatorClause.html +1 -1
- package/docs/types/OptionalUnlessRequiredId.html +1 -1
- package/docs/types/OrderClause.html +1 -1
- package/docs/types/PartialBy.html +1 -1
- package/docs/types/PartialWithoutId.html +1 -1
- package/docs/types/PolyBelongsToDefinition.html +1 -1
- package/docs/types/PolyHasManyDefinitionWithTargetKeys.html +1 -1
- package/docs/types/PolyHasManyDefinitionWithTargetRelationName.html +1 -1
- package/docs/types/PolyHasOneDefinitionWithTargetKeys.html +1 -1
- package/docs/types/PolyHasOneDefinitionWithTargetRelationName.html +1 -1
- package/docs/types/PropertiesClause.html +1 -1
- package/docs/types/PropertyDefinition.html +1 -1
- package/docs/types/PropertyDefinitionMap.html +1 -1
- package/docs/types/PropertyValidateOptions.html +2 -0
- package/docs/types/PropertyValidator.html +2 -0
- package/docs/types/PropertyValidatorContext.html +2 -0
- package/docs/types/ReferencesManyDefinition.html +1 -1
- package/docs/types/RelationDefinition.html +1 -1
- package/docs/types/RelationDefinitionMap.html +1 -1
- package/docs/types/WhereClause.html +1 -1
- package/docs/types/WithoutId.html +1 -1
- package/package.json +2 -2
- package/src/definition/model/model-data-validator.js +7 -17
- package/src/definition/model/model-data-validator.spec.js +1577 -1543
- package/src/definition/model/properties/property-validator/builtin/index.d.ts +3 -0
- package/src/definition/model/properties/property-validator/builtin/index.js +3 -0
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.d.ts +6 -0
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.js +28 -0
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.spec.js +93 -0
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.d.ts +6 -0
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.js +28 -0
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js +93 -0
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.d.ts +6 -0
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.js +30 -0
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.spec.js +95 -0
- package/src/definition/model/properties/property-validator/property-validator-registry.js +8 -1
- package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +1 -1
- package/src/schema.d.ts +0 -9
- package/src/schema.js +0 -13
- package/src/schema.spec.ts +0 -29
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {InvalidArgumentError} from '../../../../../errors/index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Max length validator.
|
|
5
|
+
*
|
|
6
|
+
* @param {*} value
|
|
7
|
+
* @param {number|boolean} options
|
|
8
|
+
* @param {object} context
|
|
9
|
+
* @returns {boolean}
|
|
10
|
+
*/
|
|
11
|
+
export function maxLengthValidator(value, options, context) {
|
|
12
|
+
if (options === false) return true;
|
|
13
|
+
if (typeof options !== 'number')
|
|
14
|
+
throw new InvalidArgumentError(
|
|
15
|
+
'The validator %v requires the "options" argument ' +
|
|
16
|
+
'as a Number, but %v given.',
|
|
17
|
+
context.validatorName,
|
|
18
|
+
options,
|
|
19
|
+
);
|
|
20
|
+
if (typeof value === 'string' || Array.isArray(value))
|
|
21
|
+
return value.length <= options;
|
|
22
|
+
throw new InvalidArgumentError(
|
|
23
|
+
'The property validator %v requires a String ' +
|
|
24
|
+
'or an Array value, but %v given.',
|
|
25
|
+
context.validatorName,
|
|
26
|
+
value,
|
|
27
|
+
);
|
|
28
|
+
}
|
package/src/definition/model/properties/property-validator/builtin/max-length-validator.spec.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {expect} from 'chai';
|
|
2
|
+
import {format} from '@e22m4u/js-format';
|
|
3
|
+
import {maxLengthValidator} from './max-length-validator.js';
|
|
4
|
+
|
|
5
|
+
describe('maxLengthValidator', function () {
|
|
6
|
+
it('returns true if the "options" argument is false', function () {
|
|
7
|
+
const res = maxLengthValidator(undefined, false, {});
|
|
8
|
+
expect(res).to.be.true;
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('requires the "value" argument as a String or an Array', function () {
|
|
12
|
+
const throwable = v => () =>
|
|
13
|
+
maxLengthValidator(v, 10, {
|
|
14
|
+
validatorName: 'myValidator',
|
|
15
|
+
});
|
|
16
|
+
const error = v =>
|
|
17
|
+
format(
|
|
18
|
+
'The property validator "myValidator" requires a String ' +
|
|
19
|
+
'or an Array value, but %s given.',
|
|
20
|
+
v,
|
|
21
|
+
);
|
|
22
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
23
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
24
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
25
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
26
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
27
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
28
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
29
|
+
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
30
|
+
throwable('str')();
|
|
31
|
+
throwable('')();
|
|
32
|
+
throwable([1, 2, 3])();
|
|
33
|
+
throwable([])();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('requires the "options" argument to be a number', function () {
|
|
37
|
+
const throwable = v => () =>
|
|
38
|
+
maxLengthValidator('test', v, {
|
|
39
|
+
validatorName: 'myValidator',
|
|
40
|
+
});
|
|
41
|
+
const error = v =>
|
|
42
|
+
format(
|
|
43
|
+
'The validator "myValidator" requires the "options" argument ' +
|
|
44
|
+
'as a Number, but %s given.',
|
|
45
|
+
v,
|
|
46
|
+
);
|
|
47
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
48
|
+
expect(throwable('')).to.throw(error('""'));
|
|
49
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
50
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
51
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
52
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
53
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
54
|
+
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
55
|
+
throwable(10)();
|
|
56
|
+
throwable(0)();
|
|
57
|
+
throwable(false)();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('a string value', function () {
|
|
61
|
+
it('returns false if the value length is greater than the max length option', function () {
|
|
62
|
+
const res = maxLengthValidator('1234', 3, {});
|
|
63
|
+
expect(res).to.be.false;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('returns true if the value length is equal to the max length option', function () {
|
|
67
|
+
const res = maxLengthValidator('123', 3, {});
|
|
68
|
+
expect(res).to.be.true;
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('returns true if the value length is lower than the max length option', function () {
|
|
72
|
+
const res = maxLengthValidator('12', 3, {});
|
|
73
|
+
expect(res).to.be.true;
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('an array value', function () {
|
|
78
|
+
it('returns false if the value length is greater than the max length option', function () {
|
|
79
|
+
const res = maxLengthValidator([1, 2, 3, 4], 3, {});
|
|
80
|
+
expect(res).to.be.false;
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('returns true if the value length is equal to the max length option', function () {
|
|
84
|
+
const res = maxLengthValidator([1, 2, 3], 3, {});
|
|
85
|
+
expect(res).to.be.true;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('returns true if the value length is lower than the max length option', function () {
|
|
89
|
+
const res = maxLengthValidator([1, 2], 3, {});
|
|
90
|
+
expect(res).to.be.true;
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {InvalidArgumentError} from '../../../../../errors/index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Min length validator.
|
|
5
|
+
*
|
|
6
|
+
* @param {*} value
|
|
7
|
+
* @param {number|boolean} options
|
|
8
|
+
* @param {object} context
|
|
9
|
+
* @returns {boolean}
|
|
10
|
+
*/
|
|
11
|
+
export function minLengthValidator(value, options, context) {
|
|
12
|
+
if (options === false) return true;
|
|
13
|
+
if (typeof options !== 'number')
|
|
14
|
+
throw new InvalidArgumentError(
|
|
15
|
+
'The validator %v requires the "options" argument ' +
|
|
16
|
+
'as a Number, but %v given.',
|
|
17
|
+
context.validatorName,
|
|
18
|
+
options,
|
|
19
|
+
);
|
|
20
|
+
if (typeof value === 'string' || Array.isArray(value))
|
|
21
|
+
return value.length >= options;
|
|
22
|
+
throw new InvalidArgumentError(
|
|
23
|
+
'The property validator %v requires a String ' +
|
|
24
|
+
'or an Array value, but %v given.',
|
|
25
|
+
context.validatorName,
|
|
26
|
+
value,
|
|
27
|
+
);
|
|
28
|
+
}
|
package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {expect} from 'chai';
|
|
2
|
+
import {format} from '@e22m4u/js-format';
|
|
3
|
+
import {minLengthValidator} from './min-length-validator.js';
|
|
4
|
+
|
|
5
|
+
describe('minLengthValidator', function () {
|
|
6
|
+
it('returns true if the "options" argument is false', function () {
|
|
7
|
+
const res = minLengthValidator(undefined, false, {});
|
|
8
|
+
expect(res).to.be.true;
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('requires the "value" argument as a String or an Array', function () {
|
|
12
|
+
const throwable = v => () =>
|
|
13
|
+
minLengthValidator(v, 0, {
|
|
14
|
+
validatorName: 'myValidator',
|
|
15
|
+
});
|
|
16
|
+
const error = v =>
|
|
17
|
+
format(
|
|
18
|
+
'The property validator "myValidator" requires a String ' +
|
|
19
|
+
'or an Array value, but %s given.',
|
|
20
|
+
v,
|
|
21
|
+
);
|
|
22
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
23
|
+
expect(throwable(0)).to.throw(error('0'));
|
|
24
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
25
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
26
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
27
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
28
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
29
|
+
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
30
|
+
throwable('str')();
|
|
31
|
+
throwable('')();
|
|
32
|
+
throwable([1, 2, 3])();
|
|
33
|
+
throwable([])();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('requires the "options" argument to be a number', function () {
|
|
37
|
+
const throwable = v => () =>
|
|
38
|
+
minLengthValidator('test', v, {
|
|
39
|
+
validatorName: 'myValidator',
|
|
40
|
+
});
|
|
41
|
+
const error = v =>
|
|
42
|
+
format(
|
|
43
|
+
'The validator "myValidator" requires the "options" argument ' +
|
|
44
|
+
'as a Number, but %s given.',
|
|
45
|
+
v,
|
|
46
|
+
);
|
|
47
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
48
|
+
expect(throwable('')).to.throw(error('""'));
|
|
49
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
50
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
51
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
52
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
53
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
54
|
+
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
55
|
+
throwable(10)();
|
|
56
|
+
throwable(0)();
|
|
57
|
+
throwable(false)();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('a string value', function () {
|
|
61
|
+
it('returns false if the value length is lower than the min length option', function () {
|
|
62
|
+
const res = minLengthValidator('12', 3, {});
|
|
63
|
+
expect(res).to.be.false;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('returns true if the value length is equal to the min length option', function () {
|
|
67
|
+
const res = minLengthValidator('123', 3, {});
|
|
68
|
+
expect(res).to.be.true;
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('returns true if the value length is greater than the min length option', function () {
|
|
72
|
+
const res = minLengthValidator('1234', 3, {});
|
|
73
|
+
expect(res).to.be.true;
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('an array value', function () {
|
|
78
|
+
it('returns false if the value length is lower than the min length option', function () {
|
|
79
|
+
const res = minLengthValidator([1, 2], 3, {});
|
|
80
|
+
expect(res).to.be.false;
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('returns true if the value length is equal to the min length option', function () {
|
|
84
|
+
const res = minLengthValidator([1, 2, 3], 3, {});
|
|
85
|
+
expect(res).to.be.true;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('returns true if the value length is greater than the min length option', function () {
|
|
89
|
+
const res = minLengthValidator([1, 2, 3, 4], 3, {});
|
|
90
|
+
expect(res).to.be.true;
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {stringToRegexp} from '../../../../../utils/index.js';
|
|
2
|
+
import {InvalidArgumentError} from '../../../../../errors/index.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Regexp validator.
|
|
6
|
+
*
|
|
7
|
+
* @param {*} value
|
|
8
|
+
* @param {string|RegExp|boolean} options
|
|
9
|
+
* @param {object} context
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
12
|
+
export function regexpValidator(value, options, context) {
|
|
13
|
+
if (options === false) return true;
|
|
14
|
+
if (typeof options !== 'string' && !(options instanceof RegExp))
|
|
15
|
+
throw new InvalidArgumentError(
|
|
16
|
+
'The validator %v requires the "options" argument ' +
|
|
17
|
+
'as a String or RegExp, but %v given.',
|
|
18
|
+
context.validatorName,
|
|
19
|
+
options,
|
|
20
|
+
);
|
|
21
|
+
if (typeof value === 'string') {
|
|
22
|
+
const regexp = stringToRegexp(options);
|
|
23
|
+
return regexp.test(value);
|
|
24
|
+
}
|
|
25
|
+
throw new InvalidArgumentError(
|
|
26
|
+
'The property validator %v requires ' + 'a String value, but %v given.',
|
|
27
|
+
context.validatorName,
|
|
28
|
+
value,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {expect} from 'chai';
|
|
2
|
+
import {format} from '@e22m4u/js-format';
|
|
3
|
+
import {regexpValidator} from './regexp-validator.js';
|
|
4
|
+
|
|
5
|
+
describe('regexpValidator', function () {
|
|
6
|
+
it('returns true if the "options" argument is false', function () {
|
|
7
|
+
const res = regexpValidator(undefined, false, {});
|
|
8
|
+
expect(res).to.be.true;
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('requires the "value" argument to be a string', function () {
|
|
12
|
+
const throwable = v => () =>
|
|
13
|
+
regexpValidator(v, '.*', {
|
|
14
|
+
validatorName: 'myValidator',
|
|
15
|
+
});
|
|
16
|
+
const error = v =>
|
|
17
|
+
format(
|
|
18
|
+
'The property validator "myValidator" requires ' +
|
|
19
|
+
'a String value, but %s given.',
|
|
20
|
+
v,
|
|
21
|
+
);
|
|
22
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
23
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
24
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
25
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
26
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
27
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
28
|
+
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
29
|
+
throwable('str')();
|
|
30
|
+
throwable('')();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('requires the "options" argument to be a string or RegExp', function () {
|
|
34
|
+
const throwable = v => () =>
|
|
35
|
+
regexpValidator('test', v, {
|
|
36
|
+
validatorName: 'myValidator',
|
|
37
|
+
});
|
|
38
|
+
const error = v =>
|
|
39
|
+
format(
|
|
40
|
+
'The validator "myValidator" requires the "options" argument ' +
|
|
41
|
+
'as a String or RegExp, but %s given.',
|
|
42
|
+
v,
|
|
43
|
+
);
|
|
44
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
45
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
46
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
47
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
48
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
49
|
+
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
50
|
+
throwable('str')();
|
|
51
|
+
throwable('')();
|
|
52
|
+
throwable(false)();
|
|
53
|
+
throwable(new RegExp(''))();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('the given regexp as a String', function () {
|
|
57
|
+
it('returns false if the given regexp is not matched', function () {
|
|
58
|
+
const res1 = regexpValidator('foo', 'bar', {});
|
|
59
|
+
const res2 = regexpValidator('foo', '^\\d+', {});
|
|
60
|
+
const res3 = regexpValidator('foo', '^baz$', {});
|
|
61
|
+
expect(res1).to.be.false;
|
|
62
|
+
expect(res2).to.be.false;
|
|
63
|
+
expect(res3).to.be.false;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('returns true if the given regexp is matched', function () {
|
|
67
|
+
const res1 = regexpValidator('foo', '^fo', {});
|
|
68
|
+
const res2 = regexpValidator('foo', '^foo$', {});
|
|
69
|
+
const res3 = regexpValidator('foo', '^\\w+$', {});
|
|
70
|
+
expect(res1).to.be.true;
|
|
71
|
+
expect(res2).to.be.true;
|
|
72
|
+
expect(res3).to.be.true;
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('the given regexp as an instance of RegExp', function () {
|
|
77
|
+
it('returns false if the given regexp is not matched', function () {
|
|
78
|
+
const res1 = regexpValidator('foo', /bar/, {});
|
|
79
|
+
const res2 = regexpValidator('foo', /^\d+/, {});
|
|
80
|
+
const res3 = regexpValidator('foo', /^bar$/, {});
|
|
81
|
+
expect(res1).to.be.false;
|
|
82
|
+
expect(res2).to.be.false;
|
|
83
|
+
expect(res3).to.be.false;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('returns true if the given regexp is matched', function () {
|
|
87
|
+
const res1 = regexpValidator('foo', /^fo/, {});
|
|
88
|
+
const res2 = regexpValidator('foo', /^foo$/, {});
|
|
89
|
+
const res3 = regexpValidator('foo', /^\w+$/, {});
|
|
90
|
+
expect(res1).to.be.true;
|
|
91
|
+
expect(res2).to.be.true;
|
|
92
|
+
expect(res3).to.be.true;
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import {Service} from '@e22m4u/js-service';
|
|
2
|
+
import {regexpValidator} from './builtin/index.js';
|
|
3
|
+
import {maxLengthValidator} from './builtin/index.js';
|
|
4
|
+
import {minLengthValidator} from './builtin/index.js';
|
|
2
5
|
import {InvalidArgumentError} from '../../../../errors/index.js';
|
|
3
6
|
|
|
4
7
|
/**
|
|
@@ -10,7 +13,11 @@ export class PropertyValidatorRegistry extends Service {
|
|
|
10
13
|
*
|
|
11
14
|
* @type {object}
|
|
12
15
|
*/
|
|
13
|
-
_validators = {
|
|
16
|
+
_validators = {
|
|
17
|
+
maxLength: maxLengthValidator,
|
|
18
|
+
minLength: minLengthValidator,
|
|
19
|
+
regexp: regexpValidator,
|
|
20
|
+
};
|
|
14
21
|
|
|
15
22
|
/**
|
|
16
23
|
* Add validator.
|
package/src/definition/model/properties/property-validator/property-validator-registry.spec.js
CHANGED
|
@@ -9,7 +9,7 @@ describe('PropertyValidatorRegistry', function () {
|
|
|
9
9
|
const myValidator = () => {};
|
|
10
10
|
const res = s.addValidator('myValidator', myValidator);
|
|
11
11
|
expect(res).to.be.eq(s);
|
|
12
|
-
expect(s['_validators']).to.be.
|
|
12
|
+
expect(s['_validators']['myValidator']).to.be.eq(myValidator);
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
it('requires the given name to be a non-empty string', function () {
|
package/src/schema.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import {ModelData} from './types.js';
|
|
|
3
3
|
import {Service} from '@e22m4u/js-service';
|
|
4
4
|
import {Repository} from './repository/index.js';
|
|
5
5
|
import {ModelDefinition} from './definition/index.js';
|
|
6
|
-
import {PropertyValidator} from './definition/index.js';
|
|
7
6
|
import {DatasourceDefinition} from './definition/index.js';
|
|
8
7
|
import {DEFAULT_PRIMARY_KEY_PROPERTY_NAME} from './definition/index.js';
|
|
9
8
|
|
|
@@ -35,12 +34,4 @@ export declare class Schema extends Service {
|
|
|
35
34
|
IdType extends ModelId = ModelId,
|
|
36
35
|
IdName extends string = DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
|
|
37
36
|
>(modelName: string): Repository<Data, IdType, IdName>;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Add property validator.
|
|
41
|
-
*
|
|
42
|
-
* @param name
|
|
43
|
-
* @param validator
|
|
44
|
-
*/
|
|
45
|
-
addPropertyValidator(name: string, validator: PropertyValidator): this;
|
|
46
37
|
}
|
package/src/schema.js
CHANGED
|
@@ -2,7 +2,6 @@ import {Service} from '@e22m4u/js-service';
|
|
|
2
2
|
import {Repository} from './repository/index.js';
|
|
3
3
|
import {DefinitionRegistry} from './definition/index.js';
|
|
4
4
|
import {RepositoryRegistry} from './repository/index.js';
|
|
5
|
-
import {PropertyValidatorRegistry} from './definition/index.js';
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Schema.
|
|
@@ -39,16 +38,4 @@ export class Schema extends Service {
|
|
|
39
38
|
getRepository(modelName) {
|
|
40
39
|
return this.getService(RepositoryRegistry).getRepository(modelName);
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Add property validator.
|
|
45
|
-
*
|
|
46
|
-
* @param {string} name
|
|
47
|
-
* @param {Function} validator
|
|
48
|
-
* @returns {this}
|
|
49
|
-
*/
|
|
50
|
-
addPropertyValidator(name, validator) {
|
|
51
|
-
this.getService(PropertyValidatorRegistry).addValidator(name, validator);
|
|
52
|
-
return this;
|
|
53
|
-
}
|
|
54
41
|
}
|
package/src/schema.spec.ts
CHANGED
|
@@ -2,7 +2,6 @@ import {expect} from 'chai';
|
|
|
2
2
|
import {Schema} from './schema.js';
|
|
3
3
|
import {Repository} from './repository/index.js';
|
|
4
4
|
import {DefinitionRegistry} from './definition/index.js';
|
|
5
|
-
import {PropertyValidatorRegistry} from './definition/index.js';
|
|
6
5
|
|
|
7
6
|
describe('Schema', function () {
|
|
8
7
|
describe('defineDatasource', function () {
|
|
@@ -84,32 +83,4 @@ describe('Schema', function () {
|
|
|
84
83
|
expect(res1).to.be.eq(res2);
|
|
85
84
|
});
|
|
86
85
|
});
|
|
87
|
-
|
|
88
|
-
describe('addPropertyValidator', function () {
|
|
89
|
-
it('returns this', function () {
|
|
90
|
-
const schema = new Schema();
|
|
91
|
-
const res = schema.addPropertyValidator('validator', () => true);
|
|
92
|
-
expect(res).to.be.eq(schema);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('adds a new property validator', function () {
|
|
96
|
-
const schema = new Schema();
|
|
97
|
-
const validator = () => true;
|
|
98
|
-
schema.addPropertyValidator('validator', validator);
|
|
99
|
-
const res = schema.getService(PropertyValidatorRegistry)
|
|
100
|
-
.getValidator('validator');
|
|
101
|
-
expect(res).to.be.eq(validator);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('throws an error for already existing validator name', function () {
|
|
105
|
-
const schema = new Schema();
|
|
106
|
-
const validator = () => true;
|
|
107
|
-
schema.addPropertyValidator('validator', validator);
|
|
108
|
-
const throwable =
|
|
109
|
-
() => schema.addPropertyValidator('validator', validator);
|
|
110
|
-
expect(throwable).to.throw(
|
|
111
|
-
'The property validator "validator" is already defined.',
|
|
112
|
-
);
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
86
|
});
|