@e22m4u/js-repository 0.1.4 → 0.1.6

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.
Files changed (22) hide show
  1. package/README.md +6 -5
  2. package/docs/assets/search.js +1 -1
  3. package/docs/classes/ModelDataValidator.html +2 -2
  4. package/docs/index.html +7 -5
  5. package/docs/types/PropertyValidateOptions.html +1 -1
  6. package/docs/types/PropertyValidator.html +2 -2
  7. package/docs/types/PropertyValidatorContext.html +2 -2
  8. package/package.json +1 -1
  9. package/src/adapter/decorator/data-validation-decorator.js +10 -18
  10. package/src/adapter/decorator/data-validation-decorator.spec.js +0 -40
  11. package/src/definition/model/model-data-validator.d.ts +1 -5
  12. package/src/definition/model/model-data-validator.js +44 -49
  13. package/src/definition/model/model-data-validator.spec.js +1794 -1840
  14. package/src/definition/model/properties/property-validator/builtin/index.d.ts +1 -0
  15. package/src/definition/model/properties/property-validator/builtin/index.js +1 -0
  16. package/src/definition/model/properties/property-validator/builtin/max-length-validator.spec.js +29 -1
  17. package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js +29 -1
  18. package/src/definition/model/properties/property-validator/builtin/regexp-validator.d.ts +6 -0
  19. package/src/definition/model/properties/property-validator/builtin/regexp-validator.js +30 -0
  20. package/src/definition/model/properties/property-validator/builtin/regexp-validator.spec.js +95 -0
  21. package/src/definition/model/properties/property-validator/property-validator-registry.js +2 -0
  22. package/src/definition/model/properties/property-validator/property-validator.d.ts +1 -6
@@ -1,2 +1,3 @@
1
+ export * from './regexp-validator.js';
1
2
  export * from './max-length-validator.js';
2
3
  export * from './min-length-validator.js';
@@ -1,2 +1,3 @@
1
+ export * from './regexp-validator.js';
1
2
  export * from './max-length-validator.js';
2
3
  export * from './min-length-validator.js';
@@ -3,11 +3,36 @@ import {format} from '@e22m4u/js-format';
3
3
  import {maxLengthValidator} from './max-length-validator.js';
4
4
 
5
5
  describe('maxLengthValidator', function () {
6
- it('returns true by the false value in options', function () {
6
+ it('returns true if the "options" argument is false', function () {
7
7
  const res = maxLengthValidator(undefined, false, {});
8
8
  expect(res).to.be.true;
9
9
  });
10
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
+
11
36
  it('requires the "options" argument to be a number', function () {
12
37
  const throwable = v => () =>
13
38
  maxLengthValidator('test', v, {
@@ -27,6 +52,9 @@ describe('maxLengthValidator', function () {
27
52
  expect(throwable({})).to.throw(error('Object'));
28
53
  expect(throwable([])).to.throw(error('Array'));
29
54
  expect(throwable(() => undefined)).to.throw(error('Function'));
55
+ throwable(10)();
56
+ throwable(0)();
57
+ throwable(false)();
30
58
  });
31
59
 
32
60
  describe('a string value', function () {
@@ -3,11 +3,36 @@ import {format} from '@e22m4u/js-format';
3
3
  import {minLengthValidator} from './min-length-validator.js';
4
4
 
5
5
  describe('minLengthValidator', function () {
6
- it('returns true by the false value in options', function () {
6
+ it('returns true if the "options" argument is false', function () {
7
7
  const res = minLengthValidator(undefined, false, {});
8
8
  expect(res).to.be.true;
9
9
  });
10
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
+
11
36
  it('requires the "options" argument to be a number', function () {
12
37
  const throwable = v => () =>
13
38
  minLengthValidator('test', v, {
@@ -27,6 +52,9 @@ describe('minLengthValidator', function () {
27
52
  expect(throwable({})).to.throw(error('Object'));
28
53
  expect(throwable([])).to.throw(error('Array'));
29
54
  expect(throwable(() => undefined)).to.throw(error('Function'));
55
+ throwable(10)();
56
+ throwable(0)();
57
+ throwable(false)();
30
58
  });
31
59
 
32
60
  describe('a string value', function () {
@@ -0,0 +1,6 @@
1
+ import {PropertyValidator} from '../property-validator.js';
2
+
3
+ /**
4
+ * Regexp validator.
5
+ */
6
+ export declare type regexpValidator = PropertyValidator;
@@ -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,5 @@
1
1
  import {Service} from '@e22m4u/js-service';
2
+ import {regexpValidator} from './builtin/index.js';
2
3
  import {maxLengthValidator} from './builtin/index.js';
3
4
  import {minLengthValidator} from './builtin/index.js';
4
5
  import {InvalidArgumentError} from '../../../../errors/index.js';
@@ -15,6 +16,7 @@ export class PropertyValidatorRegistry extends Service {
15
16
  _validators = {
16
17
  maxLength: maxLengthValidator,
17
18
  minLength: minLengthValidator,
19
+ regexp: regexpValidator,
18
20
  };
19
21
 
20
22
  /**
@@ -1,6 +1,3 @@
1
- import {ServiceContainer} from '@e22m4u/js-service';
2
- import {FullPropertyDefinition} from '../property-definition.js';
3
-
4
1
  /**
5
2
  * Property validator context.
6
3
  */
@@ -8,8 +5,6 @@ export type PropertyValidatorContext = {
8
5
  validatorName: string,
9
6
  modelName: string,
10
7
  propName: string,
11
- propDef: FullPropertyDefinition,
12
- container: ServiceContainer,
13
8
  }
14
9
 
15
10
  /**
@@ -19,7 +14,7 @@ export type PropertyValidator = (
19
14
  value: unknown,
20
15
  options: unknown,
21
16
  context: PropertyValidatorContext,
22
- ) => Promise<boolean> | boolean;
17
+ ) => boolean;
23
18
 
24
19
  /**
25
20
  * Property validate options.