@e22m4u/js-repository 0.1.5 → 0.1.7
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 +35 -4
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/Adapter.html +1 -1
- package/docs/classes/AdapterLoader.html +1 -1
- package/docs/classes/AdapterRegistry.html +1 -1
- package/docs/classes/BelongsToResolver.html +1 -1
- package/docs/classes/DatasourceDefinitionValidator.html +1 -1
- package/docs/classes/DefinitionRegistry.html +1 -1
- package/docs/classes/FieldsClauseTool.html +1 -1
- package/docs/classes/HasManyResolver.html +1 -1
- package/docs/classes/HasOneResolver.html +1 -1
- package/docs/classes/IncludeClauseTool.html +1 -1
- package/docs/classes/InvalidArgumentError.html +1 -1
- package/docs/classes/InvalidOperatorValueError.html +1 -1
- package/docs/classes/ModelDataSanitizer.html +1 -1
- package/docs/classes/ModelDataTransformer.html +16 -0
- package/docs/classes/ModelDataValidator.html +2 -2
- package/docs/classes/ModelDefinitionUtils.html +1 -1
- package/docs/classes/ModelDefinitionValidator.html +1 -1
- package/docs/classes/NotImplementedError.html +1 -1
- package/docs/classes/OperatorClauseTool.html +1 -1
- package/docs/classes/OrderClauseTool.html +1 -1
- package/docs/classes/PrimaryKeysDefinitionValidator.html +1 -1
- package/docs/classes/PropertiesDefinitionValidator.html +1 -1
- package/docs/classes/PropertyTransformerRegistry.html +20 -0
- package/docs/classes/PropertyValidatorRegistry.html +1 -1
- package/docs/classes/ReferencesManyResolver.html +1 -1
- package/docs/classes/RelationsDefinitionValidator.html +1 -1
- package/docs/classes/Repository.html +1 -1
- package/docs/classes/RepositoryRegistry.html +1 -1
- package/docs/classes/Schema.html +1 -1
- package/docs/classes/SliceClauseTool.html +1 -1
- package/docs/classes/WhereClauseTool.html +1 -1
- package/docs/enums/DataType.html +1 -1
- package/docs/enums/DecoratorTargetType.html +1 -1
- package/docs/enums/RelationType.html +1 -1
- 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 +20 -5
- package/docs/interfaces/AndClause.html +1 -1
- package/docs/interfaces/Constructor.html +1 -1
- package/docs/interfaces/OrClause.html +1 -1
- package/docs/modules.html +6 -1
- 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 +1 -1
- 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/PropertyTransformOptions.html +2 -0
- package/docs/types/PropertyTransformer.html +2 -0
- package/docs/types/PropertyTransformerContext.html +2 -0
- package/docs/types/PropertyValidateOptions.html +1 -1
- package/docs/types/PropertyValidator.html +2 -2
- package/docs/types/PropertyValidatorContext.html +2 -2
- 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 +3 -3
- package/src/adapter/adapter.js +2 -0
- package/src/adapter/adapter.spec.js +9 -3
- package/src/adapter/decorator/data-transformation-decorator.d.ts +14 -0
- package/src/adapter/decorator/data-transformation-decorator.js +54 -0
- package/src/adapter/decorator/data-transformation-decorator.spec.js +95 -0
- package/src/adapter/decorator/data-validation-decorator.js +11 -18
- package/src/adapter/decorator/data-validation-decorator.spec.js +48 -60
- package/src/adapter/decorator/default-values-decorator.spec.js +89 -113
- package/src/adapter/decorator/index.d.ts +1 -0
- package/src/adapter/decorator/index.js +1 -0
- package/src/definition/model/index.d.ts +1 -0
- package/src/definition/model/index.js +1 -0
- package/src/definition/model/model-data-transformer.d.ts +15 -0
- package/src/definition/model/model-data-transformer.js +96 -0
- package/src/definition/model/model-data-transformer.spec.js +534 -0
- package/src/definition/model/model-data-validator.d.ts +1 -5
- package/src/definition/model/model-data-validator.js +51 -53
- package/src/definition/model/model-data-validator.spec.js +772 -719
- package/src/definition/model/properties/index.d.ts +1 -0
- package/src/definition/model/properties/index.js +1 -0
- package/src/definition/model/properties/properties-definition-validator.js +47 -0
- package/src/definition/model/properties/properties-definition-validator.spec.js +52 -0
- package/src/definition/model/properties/property-transformer/builtin/index.d.ts +1 -0
- package/src/definition/model/properties/property-transformer/builtin/index.js +1 -0
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.d.ts +6 -0
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.js +19 -0
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.spec.js +39 -0
- package/src/definition/model/properties/property-transformer/index.d.ts +2 -0
- package/src/definition/model/properties/property-transformer/index.js +2 -0
- package/src/definition/model/properties/property-transformer/property-transformer-registry.d.ts +29 -0
- package/src/definition/model/properties/property-transformer/property-transformer-registry.js +72 -0
- package/src/definition/model/properties/property-transformer/property-transformer-registry.spec.js +129 -0
- package/src/definition/model/properties/property-transformer/property-transformer.d.ts +25 -0
- package/src/definition/model/properties/property-transformer/property-transformer.js +1 -0
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.js +1 -1
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.spec.js +10 -3
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.js +1 -1
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js +10 -3
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.js +1 -1
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.spec.js +10 -3
- package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +51 -39
- package/src/definition/model/properties/property-validator/property-validator.d.ts +1 -6
|
@@ -16,9 +16,9 @@ export class ModelDataValidator extends Service {
|
|
|
16
16
|
* @param {string} modelName
|
|
17
17
|
* @param {object} modelData
|
|
18
18
|
* @param {boolean} isPartial
|
|
19
|
-
* @returns {
|
|
19
|
+
* @returns {undefined}
|
|
20
20
|
*/
|
|
21
|
-
|
|
21
|
+
validate(modelName, modelData, isPartial = false) {
|
|
22
22
|
if (!isPureObject(modelData))
|
|
23
23
|
throw new InvalidArgumentError(
|
|
24
24
|
'The data of the model %v should be an Object, but %v given.',
|
|
@@ -30,16 +30,16 @@ export class ModelDataValidator extends Service {
|
|
|
30
30
|
ModelDefinitionUtils,
|
|
31
31
|
).getPropertiesDefinitionInBaseModelHierarchy(modelName);
|
|
32
32
|
const propNames = Object.keys(isPartial ? modelData : propDefs);
|
|
33
|
-
|
|
33
|
+
propNames.forEach(propName => {
|
|
34
34
|
const propDef = propDefs[propName];
|
|
35
|
-
if (!propDef)
|
|
36
|
-
|
|
35
|
+
if (!propDef) return;
|
|
36
|
+
this._validatePropertyValue(
|
|
37
37
|
modelName,
|
|
38
38
|
propName,
|
|
39
39
|
propDef,
|
|
40
40
|
modelData[propName],
|
|
41
41
|
);
|
|
42
|
-
}
|
|
42
|
+
});
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/**
|
|
@@ -49,9 +49,16 @@ export class ModelDataValidator extends Service {
|
|
|
49
49
|
* @param {string} propName
|
|
50
50
|
* @param {string|object} propDef
|
|
51
51
|
* @param {*} propValue
|
|
52
|
-
* @returns {
|
|
52
|
+
* @returns {undefined}
|
|
53
53
|
*/
|
|
54
|
-
|
|
54
|
+
_validatePropertyValue(modelName, propName, propDef, propValue) {
|
|
55
|
+
// property validators
|
|
56
|
+
this._validateValueByPropertyValidators(
|
|
57
|
+
modelName,
|
|
58
|
+
propName,
|
|
59
|
+
propDef,
|
|
60
|
+
propValue,
|
|
61
|
+
);
|
|
55
62
|
// undefined and null
|
|
56
63
|
if (propValue == null) {
|
|
57
64
|
const isRequired =
|
|
@@ -64,28 +71,21 @@ export class ModelDataValidator extends Service {
|
|
|
64
71
|
propValue,
|
|
65
72
|
);
|
|
66
73
|
}
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
// Property validators.
|
|
70
|
-
await this._validateByPropertyValidators(
|
|
71
|
-
modelName,
|
|
72
|
-
propName,
|
|
73
|
-
propDef,
|
|
74
|
-
propValue,
|
|
75
|
-
);
|
|
74
|
+
// property type
|
|
75
|
+
this._validateValueByPropertyType(modelName, propName, propDef, propValue);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
|
-
* Validate by property type.
|
|
79
|
+
* Validate value by property type.
|
|
80
80
|
*
|
|
81
81
|
* @param {string} modelName
|
|
82
82
|
* @param {string} propName
|
|
83
83
|
* @param {string|object} propDef
|
|
84
84
|
* @param {*} propValue
|
|
85
85
|
* @param {boolean} isArrayValue
|
|
86
|
-
* @returns {
|
|
86
|
+
* @returns {undefined}
|
|
87
87
|
*/
|
|
88
|
-
|
|
88
|
+
_validateValueByPropertyType(
|
|
89
89
|
modelName,
|
|
90
90
|
propName,
|
|
91
91
|
propDef,
|
|
@@ -133,8 +133,8 @@ export class ModelDataValidator extends Service {
|
|
|
133
133
|
// ARRAY
|
|
134
134
|
case DataType.ARRAY:
|
|
135
135
|
if (!Array.isArray(propValue)) throw createError('an Array');
|
|
136
|
-
|
|
137
|
-
this.
|
|
136
|
+
propValue.forEach(value =>
|
|
137
|
+
this._validateValueByPropertyType(
|
|
138
138
|
modelName,
|
|
139
139
|
propName,
|
|
140
140
|
propDef,
|
|
@@ -142,32 +142,29 @@ export class ModelDataValidator extends Service {
|
|
|
142
142
|
true,
|
|
143
143
|
),
|
|
144
144
|
);
|
|
145
|
-
await Promise.all(arrayItemsValidationPromises);
|
|
146
145
|
break;
|
|
147
146
|
// OBJECT
|
|
148
147
|
case DataType.OBJECT:
|
|
149
148
|
if (!isPureObject(propValue)) throw createError('an Object');
|
|
150
149
|
if (typeof propDef === 'object' && propDef.model)
|
|
151
|
-
|
|
150
|
+
this.validate(propDef.model, propValue);
|
|
152
151
|
break;
|
|
153
152
|
}
|
|
154
153
|
}
|
|
155
154
|
|
|
156
155
|
/**
|
|
157
|
-
* Validate by property validators.
|
|
156
|
+
* Validate value by property validators.
|
|
158
157
|
*
|
|
159
158
|
* @param {string} modelName
|
|
160
159
|
* @param {string} propName
|
|
161
160
|
* @param {string|object} propDef
|
|
162
161
|
* @param {*} propValue
|
|
163
|
-
* @returns {
|
|
162
|
+
* @returns {undefined}
|
|
164
163
|
*/
|
|
165
|
-
|
|
164
|
+
_validateValueByPropertyValidators(modelName, propName, propDef, propValue) {
|
|
166
165
|
if (typeof propDef === 'string' || propDef.validate == null) return;
|
|
167
|
-
const
|
|
168
|
-
const
|
|
169
|
-
PropertyValidatorRegistry,
|
|
170
|
-
);
|
|
166
|
+
const validateDef = propDef.validate;
|
|
167
|
+
const validatorRegistry = this.getService(PropertyValidatorRegistry);
|
|
171
168
|
const createError = validatorName =>
|
|
172
169
|
new InvalidArgumentError(
|
|
173
170
|
'The property %v of the model %v has an invalid value %v ' +
|
|
@@ -177,30 +174,31 @@ export class ModelDataValidator extends Service {
|
|
|
177
174
|
propValue,
|
|
178
175
|
validatorName,
|
|
179
176
|
);
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
177
|
+
const validateBy = (validatorName, validatorOptions = undefined) => {
|
|
178
|
+
const validator = validatorRegistry.getValidator(validatorName);
|
|
179
|
+
const context = {validatorName, modelName, propName};
|
|
180
|
+
const valid = validator(propValue, validatorOptions, context);
|
|
181
|
+
if (valid instanceof Promise) {
|
|
182
|
+
throw new InvalidArgumentError(
|
|
183
|
+
'Asynchronous property validators are not supported, ' +
|
|
184
|
+
'but the property validator %v returns a Promise.',
|
|
185
|
+
validatorName,
|
|
186
|
+
);
|
|
187
|
+
} else if (valid !== true) {
|
|
188
|
+
throw createError(validatorName);
|
|
189
|
+
}
|
|
186
190
|
};
|
|
187
|
-
if (
|
|
188
|
-
|
|
189
|
-
} else if (Array.isArray(
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
)
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
Object.keys(options).forEach(validatorName => {
|
|
197
|
-
if (Object.prototype.hasOwnProperty.call(options, validatorName)) {
|
|
198
|
-
const validatorOptions = options[validatorName];
|
|
199
|
-
const validationPromise = validateBy(validatorName, validatorOptions);
|
|
200
|
-
validationPromises.push(validationPromise);
|
|
191
|
+
if (validateDef && typeof validateDef === 'string') {
|
|
192
|
+
validateBy(validateDef);
|
|
193
|
+
} else if (Array.isArray(validateDef)) {
|
|
194
|
+
validateDef.forEach(validatorName => validateBy(validatorName));
|
|
195
|
+
} else if (validateDef !== null && typeof validateDef === 'object') {
|
|
196
|
+
Object.keys(validateDef).forEach(validatorName => {
|
|
197
|
+
if (Object.prototype.hasOwnProperty.call(validateDef, validatorName)) {
|
|
198
|
+
const validatorOptions = validateDef[validatorName];
|
|
199
|
+
validateBy(validatorName, validatorOptions);
|
|
201
200
|
}
|
|
202
201
|
});
|
|
203
|
-
await Promise.all(validationPromises);
|
|
204
202
|
} else {
|
|
205
203
|
throw new InvalidArgumentError(
|
|
206
204
|
'The provided option "validate" of the property %v in the model %v ' +
|
|
@@ -208,7 +206,7 @@ export class ModelDataValidator extends Service {
|
|
|
208
206
|
'but %v given.',
|
|
209
207
|
propName,
|
|
210
208
|
modelName,
|
|
211
|
-
|
|
209
|
+
validateDef,
|
|
212
210
|
);
|
|
213
211
|
}
|
|
214
212
|
}
|