@e22m4u/js-repository 0.1.3 → 0.1.4
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 +32 -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 +16 -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 +1 -6
- package/src/definition/model/model-data-validator.spec.js +101 -69
- package/src/definition/model/properties/property-validator/builtin/index.d.ts +2 -0
- package/src/definition/model/properties/property-validator/builtin/index.js +2 -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 +65 -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 +65 -0
- package/src/definition/model/properties/property-validator/property-validator-registry.js +6 -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
|
+
* 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,65 @@
|
|
|
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 by the false value in options', function () {
|
|
7
|
+
const res = minLengthValidator(undefined, false, {});
|
|
8
|
+
expect(res).to.be.true;
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('requires the "options" argument to be a number', function () {
|
|
12
|
+
const throwable = v => () =>
|
|
13
|
+
minLengthValidator('test', v, {
|
|
14
|
+
validatorName: 'myValidator',
|
|
15
|
+
});
|
|
16
|
+
const error = v =>
|
|
17
|
+
format(
|
|
18
|
+
'The validator "myValidator" requires the "options" argument ' +
|
|
19
|
+
'as a Number, but %s given.',
|
|
20
|
+
v,
|
|
21
|
+
);
|
|
22
|
+
expect(throwable('str')).to.throw(error('"str"'));
|
|
23
|
+
expect(throwable('')).to.throw(error('""'));
|
|
24
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
25
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
26
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
27
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
28
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
29
|
+
expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('a string value', function () {
|
|
33
|
+
it('returns false if the value length is lower than the min length option', function () {
|
|
34
|
+
const res = minLengthValidator('12', 3, {});
|
|
35
|
+
expect(res).to.be.false;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('returns true if the value length is equal to the min length option', function () {
|
|
39
|
+
const res = minLengthValidator('123', 3, {});
|
|
40
|
+
expect(res).to.be.true;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('returns true if the value length is greater than the min length option', function () {
|
|
44
|
+
const res = minLengthValidator('1234', 3, {});
|
|
45
|
+
expect(res).to.be.true;
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('an array value', function () {
|
|
50
|
+
it('returns false if the value length is lower than the min length option', function () {
|
|
51
|
+
const res = minLengthValidator([1, 2], 3, {});
|
|
52
|
+
expect(res).to.be.false;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('returns true if the value length is equal to the min length option', function () {
|
|
56
|
+
const res = minLengthValidator([1, 2, 3], 3, {});
|
|
57
|
+
expect(res).to.be.true;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('returns true if the value length is greater than the min length option', function () {
|
|
61
|
+
const res = minLengthValidator([1, 2, 3, 4], 3, {});
|
|
62
|
+
expect(res).to.be.true;
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import {Service} from '@e22m4u/js-service';
|
|
2
|
+
import {maxLengthValidator} from './builtin/index.js';
|
|
3
|
+
import {minLengthValidator} from './builtin/index.js';
|
|
2
4
|
import {InvalidArgumentError} from '../../../../errors/index.js';
|
|
3
5
|
|
|
4
6
|
/**
|
|
@@ -10,7 +12,10 @@ export class PropertyValidatorRegistry extends Service {
|
|
|
10
12
|
*
|
|
11
13
|
* @type {object}
|
|
12
14
|
*/
|
|
13
|
-
_validators = {
|
|
15
|
+
_validators = {
|
|
16
|
+
maxLength: maxLengthValidator,
|
|
17
|
+
minLength: minLengthValidator,
|
|
18
|
+
};
|
|
14
19
|
|
|
15
20
|
/**
|
|
16
21
|
* 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
|
});
|