@e22m4u/js-repository 0.6.5 → 0.7.0
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 +6 -532
- package/dist/cjs/index.cjs +125 -1140
- package/package.json +4 -4
- package/src/adapter/adapter.d.ts +2 -4
- package/src/adapter/adapter.js +2 -6
- package/src/adapter/adapter.spec.js +3 -15
- package/src/adapter/builtin/memory-adapter.js +41 -12
- package/src/adapter/builtin/memory-adapter.spec.js +47 -0
- package/src/adapter/decorator/index.d.ts +0 -2
- package/src/adapter/decorator/index.js +0 -2
- package/src/definition/model/index.d.ts +0 -2
- package/src/definition/model/index.js +0 -2
- package/src/definition/model/properties/index.d.ts +0 -2
- package/src/definition/model/properties/index.js +0 -2
- package/src/definition/model/properties/properties-definition-validator.js +0 -169
- package/src/definition/model/properties/properties-definition-validator.spec.js +0 -162
- package/src/definition/model/properties/property-definition.d.ts +1 -5
- package/src/utils/index.d.ts +0 -4
- package/src/utils/index.js +0 -4
- package/src/utils/model-name-to-model-key.js +0 -4
- package/src/utils/model-name-to-model-key.spec.js +0 -23
- package/tsconfig.json +2 -2
- package/src/adapter/decorator/data-transformation-decorator.d.ts +0 -14
- package/src/adapter/decorator/data-transformation-decorator.js +0 -54
- package/src/adapter/decorator/data-transformation-decorator.spec.js +0 -193
- package/src/adapter/decorator/data-validation-decorator.d.ts +0 -14
- package/src/adapter/decorator/data-validation-decorator.js +0 -54
- package/src/adapter/decorator/data-validation-decorator.spec.js +0 -105
- package/src/definition/model/model-data-transformer.d.ts +0 -16
- package/src/definition/model/model-data-transformer.js +0 -170
- package/src/definition/model/model-data-transformer.spec.js +0 -2312
- package/src/definition/model/model-data-validator.d.ts +0 -16
- package/src/definition/model/model-data-validator.js +0 -318
- package/src/definition/model/model-data-validator.spec.js +0 -4528
- package/src/definition/model/properties/property-transformer/builtin/index.d.ts +0 -3
- package/src/definition/model/properties/property-transformer/builtin/index.js +0 -3
- package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.d.ts +0 -6
- package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.js +0 -19
- package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.spec.js +0 -39
- package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.d.ts +0 -6
- package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.js +0 -19
- package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.spec.js +0 -39
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.d.ts +0 -6
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.js +0 -19
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.spec.js +0 -39
- package/src/definition/model/properties/property-transformer/index.d.ts +0 -2
- package/src/definition/model/properties/property-transformer/index.js +0 -2
- package/src/definition/model/properties/property-transformer/property-transformer-registry.d.ts +0 -29
- package/src/definition/model/properties/property-transformer/property-transformer-registry.js +0 -76
- package/src/definition/model/properties/property-transformer/property-transformer-registry.spec.js +0 -133
- package/src/definition/model/properties/property-transformer/property-transformer.d.ts +0 -27
- package/src/definition/model/properties/property-transformer/property-transformer.js +0 -1
- package/src/definition/model/properties/property-validator/builtin/index.d.ts +0 -3
- package/src/definition/model/properties/property-validator/builtin/index.js +0 -3
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.d.ts +0 -6
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.js +0 -28
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.spec.js +0 -100
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.d.ts +0 -6
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.js +0 -28
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js +0 -100
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.d.ts +0 -6
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.js +0 -30
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.spec.js +0 -102
- package/src/definition/model/properties/property-validator/index.d.ts +0 -2
- package/src/definition/model/properties/property-validator/index.js +0 -2
- package/src/definition/model/properties/property-validator/property-validator-registry.d.ts +0 -29
- package/src/definition/model/properties/property-validator/property-validator-registry.js +0 -76
- package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +0 -132
- package/src/definition/model/properties/property-validator/property-validator.d.ts +0 -25
- package/src/definition/model/properties/property-validator/property-validator.js +0 -1
- package/src/utils/get-ctor-name.d.ts +0 -6
- package/src/utils/get-ctor-name.js +0 -11
- package/src/utils/get-ctor-name.spec.js +0 -17
- package/src/utils/get-decorator-target-type.d.ts +0 -27
- package/src/utils/get-decorator-target-type.js +0 -63
- package/src/utils/get-decorator-target-type.spec.js +0 -80
- package/src/utils/is-ctor.d.ts +0 -8
- package/src/utils/is-ctor.js +0 -11
- package/src/utils/is-ctor.spec.js +0 -26
- package/src/utils/transform-promise.d.ts +0 -13
- package/src/utils/transform-promise.js +0 -15
- package/src/utils/transform-promise.spec.js +0 -19
|
@@ -1,28 +0,0 @@
|
|
|
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 (value == null || 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 was 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 was given.',
|
|
25
|
-
context.validatorName,
|
|
26
|
-
value,
|
|
27
|
-
);
|
|
28
|
-
}
|
package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
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('test', false, {});
|
|
8
|
-
expect(res).to.be.true;
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('returns true for undefined and null values', function () {
|
|
12
|
-
const res1 = minLengthValidator(undefined, 10, {});
|
|
13
|
-
const res2 = minLengthValidator(null, 10, {});
|
|
14
|
-
expect(res1).to.be.true;
|
|
15
|
-
expect(res2).to.be.true;
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('requires the "value" argument as a String or an Array', function () {
|
|
19
|
-
const throwable = v => () =>
|
|
20
|
-
minLengthValidator(v, 0, {
|
|
21
|
-
validatorName: 'myValidator',
|
|
22
|
-
});
|
|
23
|
-
const error = v =>
|
|
24
|
-
format(
|
|
25
|
-
'The property validator "myValidator" requires a String ' +
|
|
26
|
-
'or an Array value, but %s was given.',
|
|
27
|
-
v,
|
|
28
|
-
);
|
|
29
|
-
expect(throwable(10)).to.throw(error('10'));
|
|
30
|
-
expect(throwable(0)).to.throw(error('0'));
|
|
31
|
-
expect(throwable(true)).to.throw(error('true'));
|
|
32
|
-
expect(throwable(false)).to.throw(error('false'));
|
|
33
|
-
expect(throwable({})).to.throw(error('Object'));
|
|
34
|
-
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
35
|
-
throwable('str')();
|
|
36
|
-
throwable('')();
|
|
37
|
-
throwable([1, 2, 3])();
|
|
38
|
-
throwable([])();
|
|
39
|
-
throwable(undefined)();
|
|
40
|
-
throwable(null)();
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('requires the "options" argument to be a number', function () {
|
|
44
|
-
const throwable = v => () =>
|
|
45
|
-
minLengthValidator('test', v, {
|
|
46
|
-
validatorName: 'myValidator',
|
|
47
|
-
});
|
|
48
|
-
const error = v =>
|
|
49
|
-
format(
|
|
50
|
-
'The validator "myValidator" requires the "options" argument ' +
|
|
51
|
-
'as a Number, but %s was given.',
|
|
52
|
-
v,
|
|
53
|
-
);
|
|
54
|
-
expect(throwable('str')).to.throw(error('"str"'));
|
|
55
|
-
expect(throwable('')).to.throw(error('""'));
|
|
56
|
-
expect(throwable(true)).to.throw(error('true'));
|
|
57
|
-
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
58
|
-
expect(throwable(null)).to.throw(error('null'));
|
|
59
|
-
expect(throwable({})).to.throw(error('Object'));
|
|
60
|
-
expect(throwable([])).to.throw(error('Array'));
|
|
61
|
-
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
62
|
-
throwable(10)();
|
|
63
|
-
throwable(0)();
|
|
64
|
-
throwable(false)();
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
describe('a string value', function () {
|
|
68
|
-
it('returns false if the value length is lower than the min length option', function () {
|
|
69
|
-
const res = minLengthValidator('12', 3, {});
|
|
70
|
-
expect(res).to.be.false;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('returns true if the value length is equal to the min length option', function () {
|
|
74
|
-
const res = minLengthValidator('123', 3, {});
|
|
75
|
-
expect(res).to.be.true;
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('returns true if the value length is greater than the min length option', function () {
|
|
79
|
-
const res = minLengthValidator('1234', 3, {});
|
|
80
|
-
expect(res).to.be.true;
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe('an array value', function () {
|
|
85
|
-
it('returns false if the value length is lower than the min length option', function () {
|
|
86
|
-
const res = minLengthValidator([1, 2], 3, {});
|
|
87
|
-
expect(res).to.be.false;
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('returns true if the value length is equal to the min length option', function () {
|
|
91
|
-
const res = minLengthValidator([1, 2, 3], 3, {});
|
|
92
|
-
expect(res).to.be.true;
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('returns true if the value length is greater than the min length option', function () {
|
|
96
|
-
const res = minLengthValidator([1, 2, 3, 4], 3, {});
|
|
97
|
-
expect(res).to.be.true;
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
});
|
|
@@ -1,30 +0,0 @@
|
|
|
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 (value == null || 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 was 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 was given.',
|
|
27
|
-
context.validatorName,
|
|
28
|
-
value,
|
|
29
|
-
);
|
|
30
|
-
}
|
|
@@ -1,102 +0,0 @@
|
|
|
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('test', false, {});
|
|
8
|
-
expect(res).to.be.true;
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('returns true for undefined and null values', function () {
|
|
12
|
-
const res1 = regexpValidator(undefined, '.*', {});
|
|
13
|
-
const res2 = regexpValidator(null, '.*', {});
|
|
14
|
-
expect(res1).to.be.true;
|
|
15
|
-
expect(res2).to.be.true;
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('requires the "value" argument to be a string', function () {
|
|
19
|
-
const throwable = v => () =>
|
|
20
|
-
regexpValidator(v, '.*', {
|
|
21
|
-
validatorName: 'myValidator',
|
|
22
|
-
});
|
|
23
|
-
const error = v =>
|
|
24
|
-
format(
|
|
25
|
-
'The property validator "myValidator" requires ' +
|
|
26
|
-
'a String value, but %s was given.',
|
|
27
|
-
v,
|
|
28
|
-
);
|
|
29
|
-
expect(throwable(true)).to.throw(error('true'));
|
|
30
|
-
expect(throwable(false)).to.throw(error('false'));
|
|
31
|
-
expect(throwable({})).to.throw(error('Object'));
|
|
32
|
-
expect(throwable([])).to.throw(error('Array'));
|
|
33
|
-
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
34
|
-
throwable('str')();
|
|
35
|
-
throwable('')();
|
|
36
|
-
throwable(undefined)();
|
|
37
|
-
throwable(null)();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('requires the "options" argument to be a string or RegExp', function () {
|
|
41
|
-
const throwable = v => () =>
|
|
42
|
-
regexpValidator('test', v, {
|
|
43
|
-
validatorName: 'myValidator',
|
|
44
|
-
});
|
|
45
|
-
const error = v =>
|
|
46
|
-
format(
|
|
47
|
-
'The validator "myValidator" requires the "options" argument ' +
|
|
48
|
-
'as a String or RegExp, but %s was given.',
|
|
49
|
-
v,
|
|
50
|
-
);
|
|
51
|
-
expect(throwable(true)).to.throw(error('true'));
|
|
52
|
-
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
53
|
-
expect(throwable(null)).to.throw(error('null'));
|
|
54
|
-
expect(throwable({})).to.throw(error('Object'));
|
|
55
|
-
expect(throwable([])).to.throw(error('Array'));
|
|
56
|
-
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
57
|
-
throwable('str')();
|
|
58
|
-
throwable('')();
|
|
59
|
-
throwable(false)();
|
|
60
|
-
throwable(new RegExp(''))();
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
describe('the given regexp as a String', function () {
|
|
64
|
-
it('returns false if the given regexp is not matched', function () {
|
|
65
|
-
const res1 = regexpValidator('foo', 'bar', {});
|
|
66
|
-
const res2 = regexpValidator('foo', '^\\d+', {});
|
|
67
|
-
const res3 = regexpValidator('foo', '^baz$', {});
|
|
68
|
-
expect(res1).to.be.false;
|
|
69
|
-
expect(res2).to.be.false;
|
|
70
|
-
expect(res3).to.be.false;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('returns true if the given regexp is matched', function () {
|
|
74
|
-
const res1 = regexpValidator('foo', '^fo', {});
|
|
75
|
-
const res2 = regexpValidator('foo', '^foo$', {});
|
|
76
|
-
const res3 = regexpValidator('foo', '^\\w+$', {});
|
|
77
|
-
expect(res1).to.be.true;
|
|
78
|
-
expect(res2).to.be.true;
|
|
79
|
-
expect(res3).to.be.true;
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
describe('the given regexp as an instance of RegExp', function () {
|
|
84
|
-
it('returns false if the given regexp is not matched', function () {
|
|
85
|
-
const res1 = regexpValidator('foo', /bar/, {});
|
|
86
|
-
const res2 = regexpValidator('foo', /^\d+/, {});
|
|
87
|
-
const res3 = regexpValidator('foo', /^bar$/, {});
|
|
88
|
-
expect(res1).to.be.false;
|
|
89
|
-
expect(res2).to.be.false;
|
|
90
|
-
expect(res3).to.be.false;
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('returns true if the given regexp is matched', function () {
|
|
94
|
-
const res1 = regexpValidator('foo', /^fo/, {});
|
|
95
|
-
const res2 = regexpValidator('foo', /^foo$/, {});
|
|
96
|
-
const res3 = regexpValidator('foo', /^\w+$/, {});
|
|
97
|
-
expect(res1).to.be.true;
|
|
98
|
-
expect(res2).to.be.true;
|
|
99
|
-
expect(res3).to.be.true;
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
});
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import {Service} from '@e22m4u/js-service';
|
|
2
|
-
import {PropertyValidator} from './property-validator.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Property validator registry.
|
|
6
|
-
*/
|
|
7
|
-
export declare class PropertyValidatorRegistry extends Service {
|
|
8
|
-
/**
|
|
9
|
-
* Add validator.
|
|
10
|
-
*
|
|
11
|
-
* @param name
|
|
12
|
-
* @param validator
|
|
13
|
-
*/
|
|
14
|
-
addValidator(name: string, validator: PropertyValidator): this;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Has validator.
|
|
18
|
-
*
|
|
19
|
-
* @param name
|
|
20
|
-
*/
|
|
21
|
-
hasValidator(name: string): boolean;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Get validator.
|
|
25
|
-
*
|
|
26
|
-
* @param name
|
|
27
|
-
*/
|
|
28
|
-
getValidator(name: string): PropertyValidator;
|
|
29
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
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';
|
|
5
|
-
import {InvalidArgumentError} from '../../../../errors/index.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Property validator registry.
|
|
9
|
-
*/
|
|
10
|
-
export class PropertyValidatorRegistry extends Service {
|
|
11
|
-
/**
|
|
12
|
-
* Validators.
|
|
13
|
-
*
|
|
14
|
-
* @type {object}
|
|
15
|
-
*/
|
|
16
|
-
_validators = {
|
|
17
|
-
maxLength: maxLengthValidator,
|
|
18
|
-
minLength: minLengthValidator,
|
|
19
|
-
regexp: regexpValidator,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Add validator.
|
|
24
|
-
*
|
|
25
|
-
* @param {string} name
|
|
26
|
-
* @param {Function} validator
|
|
27
|
-
* @returns {PropertyValidatorRegistry}
|
|
28
|
-
*/
|
|
29
|
-
addValidator(name, validator) {
|
|
30
|
-
if (!name || typeof name !== 'string')
|
|
31
|
-
throw new InvalidArgumentError(
|
|
32
|
-
'A name of the property validator must ' +
|
|
33
|
-
'be a non-empty String, but %v was given.',
|
|
34
|
-
name,
|
|
35
|
-
);
|
|
36
|
-
if (name in this._validators)
|
|
37
|
-
throw new InvalidArgumentError(
|
|
38
|
-
'The property validator %v is already defined.',
|
|
39
|
-
name,
|
|
40
|
-
);
|
|
41
|
-
if (typeof validator !== 'function')
|
|
42
|
-
throw new InvalidArgumentError(
|
|
43
|
-
'The property validator %v must be a Function, but %v was given.',
|
|
44
|
-
name,
|
|
45
|
-
validator,
|
|
46
|
-
);
|
|
47
|
-
this._validators[name] = validator;
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Has validator.
|
|
53
|
-
*
|
|
54
|
-
* @param {string} name
|
|
55
|
-
* @returns {boolean}
|
|
56
|
-
*/
|
|
57
|
-
hasValidator(name) {
|
|
58
|
-
return Boolean(this._validators[name]);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Get validator.
|
|
63
|
-
*
|
|
64
|
-
* @param {string} name
|
|
65
|
-
* @returns {Function}
|
|
66
|
-
*/
|
|
67
|
-
getValidator(name) {
|
|
68
|
-
const validator = this._validators[name];
|
|
69
|
-
if (!validator)
|
|
70
|
-
throw new InvalidArgumentError(
|
|
71
|
-
'The property validator %v is not defined.',
|
|
72
|
-
name,
|
|
73
|
-
);
|
|
74
|
-
return validator;
|
|
75
|
-
}
|
|
76
|
-
}
|
package/src/definition/model/properties/property-validator/property-validator-registry.spec.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import {expect} from 'chai';
|
|
2
|
-
import {format} from '@e22m4u/js-format';
|
|
3
|
-
import {regexpValidator} from './builtin/index.js';
|
|
4
|
-
import {maxLengthValidator} from './builtin/index.js';
|
|
5
|
-
import {minLengthValidator} from './builtin/index.js';
|
|
6
|
-
import {PropertyValidatorRegistry} from './property-validator-registry.js';
|
|
7
|
-
|
|
8
|
-
describe('PropertyValidatorRegistry', function () {
|
|
9
|
-
it('has builtin validators', function () {
|
|
10
|
-
const pvr = new PropertyValidatorRegistry();
|
|
11
|
-
expect(pvr['_validators']).to.be.eql({
|
|
12
|
-
maxLength: maxLengthValidator,
|
|
13
|
-
minLength: minLengthValidator,
|
|
14
|
-
regexp: regexpValidator,
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe('addValidator', function () {
|
|
19
|
-
it('adds a given validator with the name', function () {
|
|
20
|
-
const pvr = new PropertyValidatorRegistry();
|
|
21
|
-
const myValidator = () => undefined;
|
|
22
|
-
const res = pvr.addValidator('myValidator', myValidator);
|
|
23
|
-
expect(res).to.be.eq(pvr);
|
|
24
|
-
expect(pvr['_validators']['myValidator']).to.be.eq(myValidator);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('requires the given name to be a non-empty string', function () {
|
|
28
|
-
const pvr = new PropertyValidatorRegistry();
|
|
29
|
-
const throwable = v => () => pvr.addValidator(v, () => undefined);
|
|
30
|
-
const error = v =>
|
|
31
|
-
format(
|
|
32
|
-
'A name of the property validator must ' +
|
|
33
|
-
'be a non-empty String, but %s was given.',
|
|
34
|
-
v,
|
|
35
|
-
);
|
|
36
|
-
expect(throwable('')).to.throw(error('""'));
|
|
37
|
-
expect(throwable(10)).to.throw(error('10'));
|
|
38
|
-
expect(throwable(0)).to.throw(error('0'));
|
|
39
|
-
expect(throwable(false)).to.throw(error('false'));
|
|
40
|
-
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
41
|
-
expect(throwable(null)).to.throw(error('null'));
|
|
42
|
-
expect(throwable({})).to.throw(error('Object'));
|
|
43
|
-
expect(throwable([])).to.throw(error('Array'));
|
|
44
|
-
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
45
|
-
throwable('str')();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('throws an error if the given name already exists', function () {
|
|
49
|
-
const pvr = new PropertyValidatorRegistry();
|
|
50
|
-
pvr.addValidator('test', () => undefined);
|
|
51
|
-
const throwable = () => pvr.addValidator('test', () => undefined);
|
|
52
|
-
expect(throwable).to.throw(
|
|
53
|
-
'The property validator "test" is already defined.',
|
|
54
|
-
);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('requires the given validator to be a function', function () {
|
|
58
|
-
const pvr = new PropertyValidatorRegistry();
|
|
59
|
-
const throwable = v => () => pvr.addValidator('test', v);
|
|
60
|
-
const error = v =>
|
|
61
|
-
format(
|
|
62
|
-
'The property validator "test" must be a Function, but %s was given.',
|
|
63
|
-
v,
|
|
64
|
-
);
|
|
65
|
-
expect(throwable('str')).to.throw(error('"str"'));
|
|
66
|
-
expect(throwable('')).to.throw(error('""'));
|
|
67
|
-
expect(throwable(10)).to.throw(error('10'));
|
|
68
|
-
expect(throwable(0)).to.throw(error('0'));
|
|
69
|
-
expect(throwable(false)).to.throw(error('false'));
|
|
70
|
-
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
71
|
-
expect(throwable(null)).to.throw(error('null'));
|
|
72
|
-
expect(throwable({})).to.throw(error('Object'));
|
|
73
|
-
expect(throwable([])).to.throw(error('Array'));
|
|
74
|
-
throwable(() => undefined)();
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
describe('hasValidator', function () {
|
|
79
|
-
it('returns false for a not existing name', function () {
|
|
80
|
-
const pvr = new PropertyValidatorRegistry();
|
|
81
|
-
expect(pvr.hasValidator('str')).to.be.false;
|
|
82
|
-
expect(pvr.hasValidator('')).to.be.false;
|
|
83
|
-
expect(pvr.hasValidator(10)).to.be.false;
|
|
84
|
-
expect(pvr.hasValidator(0)).to.be.false;
|
|
85
|
-
expect(pvr.hasValidator(true)).to.be.false;
|
|
86
|
-
expect(pvr.hasValidator(false)).to.be.false;
|
|
87
|
-
expect(pvr.hasValidator(null)).to.be.false;
|
|
88
|
-
expect(pvr.hasValidator(undefined)).to.be.false;
|
|
89
|
-
expect(pvr.hasValidator({})).to.be.false;
|
|
90
|
-
expect(pvr.hasValidator([])).to.be.false;
|
|
91
|
-
expect(pvr.hasValidator(() => undefined)).to.be.false;
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('returns true for an existing name', function () {
|
|
95
|
-
const pvr = new PropertyValidatorRegistry();
|
|
96
|
-
expect(pvr.hasValidator('test')).to.be.false;
|
|
97
|
-
pvr.addValidator('test', () => undefined);
|
|
98
|
-
expect(pvr.hasValidator('test')).to.be.true;
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
describe('getValidator', function () {
|
|
103
|
-
it('returns validator by its name', function () {
|
|
104
|
-
const pvr = new PropertyValidatorRegistry();
|
|
105
|
-
const myValidator1 = () => undefined;
|
|
106
|
-
const myValidator2 = () => undefined;
|
|
107
|
-
pvr.addValidator('foo', myValidator1);
|
|
108
|
-
pvr.addValidator('bar', myValidator2);
|
|
109
|
-
const res1 = pvr.getValidator('foo');
|
|
110
|
-
const res2 = pvr.getValidator('bar');
|
|
111
|
-
expect(res1).to.be.eq(myValidator1);
|
|
112
|
-
expect(res2).to.be.eq(myValidator2);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('throws an error for a not existed name', function () {
|
|
116
|
-
const pvr = new PropertyValidatorRegistry();
|
|
117
|
-
const throwable = v => () => pvr.getValidator(v);
|
|
118
|
-
const error = v => format('The property validator %s is not defined.', v);
|
|
119
|
-
expect(throwable('str')).to.throw(error('"str"'));
|
|
120
|
-
expect(throwable('')).to.throw(error('""'));
|
|
121
|
-
expect(throwable(10)).to.throw(error('10'));
|
|
122
|
-
expect(throwable(0)).to.throw(error('0'));
|
|
123
|
-
expect(throwable(true)).to.throw(error('true'));
|
|
124
|
-
expect(throwable(false)).to.throw(error('false'));
|
|
125
|
-
expect(throwable(null)).to.throw(error('null'));
|
|
126
|
-
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
127
|
-
expect(throwable({})).to.throw(error('Object'));
|
|
128
|
-
expect(throwable([])).to.throw(error('Array'));
|
|
129
|
-
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
});
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Property validator context.
|
|
3
|
-
*/
|
|
4
|
-
export type PropertyValidatorContext = {
|
|
5
|
-
validatorName: string,
|
|
6
|
-
modelName: string,
|
|
7
|
-
propName: string,
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Property validator.
|
|
12
|
-
*/
|
|
13
|
-
export type PropertyValidator = (
|
|
14
|
-
value: unknown,
|
|
15
|
-
options: unknown,
|
|
16
|
-
context: PropertyValidatorContext,
|
|
17
|
-
) => boolean;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Property validate options.
|
|
21
|
-
*/
|
|
22
|
-
export type PropertyValidateOptions =
|
|
23
|
-
| (string | PropertyValidator)
|
|
24
|
-
| (string | PropertyValidator)[]
|
|
25
|
-
| {[key: string]: unknown};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Get ctor name.
|
|
3
|
-
*
|
|
4
|
-
* @param {*} value
|
|
5
|
-
* @returns {string|undefined}
|
|
6
|
-
*/
|
|
7
|
-
export function getCtorName(value) {
|
|
8
|
-
if (value === null) return 'Null';
|
|
9
|
-
if (value === undefined) return 'Undefined';
|
|
10
|
-
return (value.constructor && value.constructor.name) || undefined;
|
|
11
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import {expect} from 'chai';
|
|
2
|
-
import {getCtorName} from './get-ctor-name.js';
|
|
3
|
-
|
|
4
|
-
describe('getCtorName', function () {
|
|
5
|
-
it('returns a constructor name of a given value', function () {
|
|
6
|
-
expect(getCtorName({})).to.be.eq('Object');
|
|
7
|
-
expect(getCtorName(new Date())).to.be.eq('Date');
|
|
8
|
-
expect(getCtorName([])).to.be.eq('Array');
|
|
9
|
-
expect(getCtorName(() => undefined)).to.be.eq('Function');
|
|
10
|
-
expect(getCtorName('string')).to.be.eq('String');
|
|
11
|
-
expect(getCtorName(10)).to.be.eq('Number');
|
|
12
|
-
expect(getCtorName(true)).to.be.eq('Boolean');
|
|
13
|
-
expect(getCtorName(false)).to.be.eq('Boolean');
|
|
14
|
-
expect(getCtorName(null)).to.be.eq('Null');
|
|
15
|
-
expect(getCtorName(undefined)).to.be.eq('Undefined');
|
|
16
|
-
});
|
|
17
|
-
});
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Decorator target type.
|
|
3
|
-
*/
|
|
4
|
-
export declare enum DecoratorTargetType {
|
|
5
|
-
CONSTRUCTOR = 'constructor',
|
|
6
|
-
INSTANCE = 'instance',
|
|
7
|
-
STATIC_METHOD = 'staticMethod',
|
|
8
|
-
INSTANCE_METHOD = 'instanceMethod',
|
|
9
|
-
STATIC_PROPERTY = 'staticProperty',
|
|
10
|
-
INSTANCE_PROPERTY = 'instanceProperty',
|
|
11
|
-
CONSTRUCTOR_PARAMETER = 'constructorParameter',
|
|
12
|
-
STATIC_METHOD_PARAMETER = 'staticMethodParameter',
|
|
13
|
-
INSTANCE_METHOD_PARAMETER = 'instanceMethodParameter',
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Get decorator target type.
|
|
18
|
-
*
|
|
19
|
-
* @param target
|
|
20
|
-
* @param propertyKey
|
|
21
|
-
* @param descriptorOrIndex
|
|
22
|
-
*/
|
|
23
|
-
export declare function getDecoratorTargetType(
|
|
24
|
-
target: object,
|
|
25
|
-
propertyKey?: string,
|
|
26
|
-
descriptorOrIndex?: PropertyDescriptor | number,
|
|
27
|
-
): DecoratorTargetType;
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {object} PropertyDescriptor
|
|
3
|
-
* @property {boolean|undefined} configurable
|
|
4
|
-
* @property {boolean|undefined} enumerable
|
|
5
|
-
* @property {*} value
|
|
6
|
-
* @property {boolean|undefined} writable
|
|
7
|
-
* @property {Function} get
|
|
8
|
-
* @property {Function} set
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Decorator target type.
|
|
13
|
-
*
|
|
14
|
-
* @enum {string}
|
|
15
|
-
* @property {string} CONSTRUCTOR
|
|
16
|
-
* @property {string} INSTANCE
|
|
17
|
-
* @property {string} STATIC_METHOD
|
|
18
|
-
* @property {string} INSTANCE_METHOD
|
|
19
|
-
* @property {string} STATIC_PROPERTY
|
|
20
|
-
* @property {string} INSTANCE_PROPERTY
|
|
21
|
-
* @property {string} CONSTRUCTOR_PARAMETER
|
|
22
|
-
* @property {string} STATIC_METHOD_PARAMETER
|
|
23
|
-
* @property {string} INSTANCE_METHOD_PARAMETER
|
|
24
|
-
*/
|
|
25
|
-
export const DecoratorTargetType = {
|
|
26
|
-
CONSTRUCTOR: 'constructor',
|
|
27
|
-
INSTANCE: 'instance',
|
|
28
|
-
STATIC_METHOD: 'staticMethod',
|
|
29
|
-
INSTANCE_METHOD: 'instanceMethod',
|
|
30
|
-
STATIC_PROPERTY: 'staticProperty',
|
|
31
|
-
INSTANCE_PROPERTY: 'instanceProperty',
|
|
32
|
-
CONSTRUCTOR_PARAMETER: 'constructorParameter',
|
|
33
|
-
STATIC_METHOD_PARAMETER: 'staticMethodParameter',
|
|
34
|
-
INSTANCE_METHOD_PARAMETER: 'instanceMethodParameter',
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Get decorator target type.
|
|
39
|
-
*
|
|
40
|
-
* @param {object} target
|
|
41
|
-
* @param {string|undefined} propertyKey
|
|
42
|
-
* @param {PropertyDescriptor|number|undefined} descriptorOrIndex
|
|
43
|
-
* @returns {DecoratorTargetType}
|
|
44
|
-
*/
|
|
45
|
-
export function getDecoratorTargetType(target, propertyKey, descriptorOrIndex) {
|
|
46
|
-
const isCtor = typeof target === 'function';
|
|
47
|
-
const isParameter = typeof descriptorOrIndex === 'number';
|
|
48
|
-
const isProperty = propertyKey != null && descriptorOrIndex == null;
|
|
49
|
-
const isMethod = propertyKey != null && descriptorOrIndex != null;
|
|
50
|
-
const D = DecoratorTargetType;
|
|
51
|
-
if (isCtor) {
|
|
52
|
-
if (isParameter)
|
|
53
|
-
return propertyKey ? D.STATIC_METHOD_PARAMETER : D.CONSTRUCTOR_PARAMETER;
|
|
54
|
-
if (isProperty) return D.STATIC_PROPERTY;
|
|
55
|
-
if (isMethod) return D.STATIC_METHOD;
|
|
56
|
-
return D.CONSTRUCTOR;
|
|
57
|
-
} else {
|
|
58
|
-
if (isParameter) return D.INSTANCE_METHOD_PARAMETER;
|
|
59
|
-
if (isProperty) return D.INSTANCE_PROPERTY;
|
|
60
|
-
if (isMethod) return D.INSTANCE_METHOD;
|
|
61
|
-
return D.INSTANCE;
|
|
62
|
-
}
|
|
63
|
-
}
|