@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.
Files changed (139) hide show
  1. package/README.md +35 -4
  2. package/docs/assets/navigation.js +1 -1
  3. package/docs/assets/search.js +1 -1
  4. package/docs/classes/Adapter.html +1 -1
  5. package/docs/classes/AdapterLoader.html +1 -1
  6. package/docs/classes/AdapterRegistry.html +1 -1
  7. package/docs/classes/BelongsToResolver.html +1 -1
  8. package/docs/classes/DatasourceDefinitionValidator.html +1 -1
  9. package/docs/classes/DefinitionRegistry.html +1 -1
  10. package/docs/classes/FieldsClauseTool.html +1 -1
  11. package/docs/classes/HasManyResolver.html +1 -1
  12. package/docs/classes/HasOneResolver.html +1 -1
  13. package/docs/classes/IncludeClauseTool.html +1 -1
  14. package/docs/classes/InvalidArgumentError.html +1 -1
  15. package/docs/classes/InvalidOperatorValueError.html +1 -1
  16. package/docs/classes/ModelDataSanitizer.html +1 -1
  17. package/docs/classes/ModelDataTransformer.html +16 -0
  18. package/docs/classes/ModelDataValidator.html +2 -2
  19. package/docs/classes/ModelDefinitionUtils.html +1 -1
  20. package/docs/classes/ModelDefinitionValidator.html +1 -1
  21. package/docs/classes/NotImplementedError.html +1 -1
  22. package/docs/classes/OperatorClauseTool.html +1 -1
  23. package/docs/classes/OrderClauseTool.html +1 -1
  24. package/docs/classes/PrimaryKeysDefinitionValidator.html +1 -1
  25. package/docs/classes/PropertiesDefinitionValidator.html +1 -1
  26. package/docs/classes/PropertyTransformerRegistry.html +20 -0
  27. package/docs/classes/PropertyValidatorRegistry.html +1 -1
  28. package/docs/classes/ReferencesManyResolver.html +1 -1
  29. package/docs/classes/RelationsDefinitionValidator.html +1 -1
  30. package/docs/classes/Repository.html +1 -1
  31. package/docs/classes/RepositoryRegistry.html +1 -1
  32. package/docs/classes/Schema.html +1 -1
  33. package/docs/classes/SliceClauseTool.html +1 -1
  34. package/docs/classes/WhereClauseTool.html +1 -1
  35. package/docs/enums/DataType.html +1 -1
  36. package/docs/enums/DecoratorTargetType.html +1 -1
  37. package/docs/enums/RelationType.html +1 -1
  38. package/docs/functions/capitalize.html +1 -1
  39. package/docs/functions/cloneDeep.html +1 -1
  40. package/docs/functions/excludeObjectKeys.html +1 -1
  41. package/docs/functions/getCtorName.html +1 -1
  42. package/docs/functions/getDecoratorTargetType.html +1 -1
  43. package/docs/functions/getValueByPath.html +1 -1
  44. package/docs/functions/isCtor.html +1 -1
  45. package/docs/functions/isPureObject.html +1 -1
  46. package/docs/functions/selectObjectKeys.html +1 -1
  47. package/docs/functions/singularize.html +1 -1
  48. package/docs/functions/stringToRegexp.html +1 -1
  49. package/docs/index.html +20 -5
  50. package/docs/interfaces/AndClause.html +1 -1
  51. package/docs/interfaces/Constructor.html +1 -1
  52. package/docs/interfaces/OrClause.html +1 -1
  53. package/docs/modules.html +6 -1
  54. package/docs/types/AnyObject.html +1 -1
  55. package/docs/types/BelongsToDefinition.html +1 -1
  56. package/docs/types/DEFAULT_PRIMARY_KEY_PROPERTY_NAME.html +1 -1
  57. package/docs/types/DatasourceDefinition.html +1 -1
  58. package/docs/types/FieldsClause.html +1 -1
  59. package/docs/types/FilterClause.html +1 -1
  60. package/docs/types/Flatten.html +1 -1
  61. package/docs/types/FullPropertyDefinition.html +1 -1
  62. package/docs/types/HasManyDefinition.html +1 -1
  63. package/docs/types/HasOneDefinition.html +1 -1
  64. package/docs/types/Identity.html +1 -1
  65. package/docs/types/IncludeClause.html +1 -1
  66. package/docs/types/ItemFilterClause.html +1 -1
  67. package/docs/types/ModelData.html +1 -1
  68. package/docs/types/ModelDefinition.html +1 -1
  69. package/docs/types/ModelId.html +1 -1
  70. package/docs/types/NestedIncludeClause.html +1 -1
  71. package/docs/types/NormalizedFieldsClause.html +1 -1
  72. package/docs/types/NormalizedIncludeClause.html +1 -1
  73. package/docs/types/OperatorClause.html +1 -1
  74. package/docs/types/OptionalUnlessRequiredId.html +1 -1
  75. package/docs/types/OrderClause.html +1 -1
  76. package/docs/types/PartialBy.html +1 -1
  77. package/docs/types/PartialWithoutId.html +1 -1
  78. package/docs/types/PolyBelongsToDefinition.html +1 -1
  79. package/docs/types/PolyHasManyDefinitionWithTargetKeys.html +1 -1
  80. package/docs/types/PolyHasManyDefinitionWithTargetRelationName.html +1 -1
  81. package/docs/types/PolyHasOneDefinitionWithTargetKeys.html +1 -1
  82. package/docs/types/PolyHasOneDefinitionWithTargetRelationName.html +1 -1
  83. package/docs/types/PropertiesClause.html +1 -1
  84. package/docs/types/PropertyDefinition.html +1 -1
  85. package/docs/types/PropertyDefinitionMap.html +1 -1
  86. package/docs/types/PropertyTransformOptions.html +2 -0
  87. package/docs/types/PropertyTransformer.html +2 -0
  88. package/docs/types/PropertyTransformerContext.html +2 -0
  89. package/docs/types/PropertyValidateOptions.html +1 -1
  90. package/docs/types/PropertyValidator.html +2 -2
  91. package/docs/types/PropertyValidatorContext.html +2 -2
  92. package/docs/types/ReferencesManyDefinition.html +1 -1
  93. package/docs/types/RelationDefinition.html +1 -1
  94. package/docs/types/RelationDefinitionMap.html +1 -1
  95. package/docs/types/WhereClause.html +1 -1
  96. package/docs/types/WithoutId.html +1 -1
  97. package/package.json +3 -3
  98. package/src/adapter/adapter.js +2 -0
  99. package/src/adapter/adapter.spec.js +9 -3
  100. package/src/adapter/decorator/data-transformation-decorator.d.ts +14 -0
  101. package/src/adapter/decorator/data-transformation-decorator.js +54 -0
  102. package/src/adapter/decorator/data-transformation-decorator.spec.js +95 -0
  103. package/src/adapter/decorator/data-validation-decorator.js +11 -18
  104. package/src/adapter/decorator/data-validation-decorator.spec.js +48 -60
  105. package/src/adapter/decorator/default-values-decorator.spec.js +89 -113
  106. package/src/adapter/decorator/index.d.ts +1 -0
  107. package/src/adapter/decorator/index.js +1 -0
  108. package/src/definition/model/index.d.ts +1 -0
  109. package/src/definition/model/index.js +1 -0
  110. package/src/definition/model/model-data-transformer.d.ts +15 -0
  111. package/src/definition/model/model-data-transformer.js +96 -0
  112. package/src/definition/model/model-data-transformer.spec.js +534 -0
  113. package/src/definition/model/model-data-validator.d.ts +1 -5
  114. package/src/definition/model/model-data-validator.js +51 -53
  115. package/src/definition/model/model-data-validator.spec.js +772 -719
  116. package/src/definition/model/properties/index.d.ts +1 -0
  117. package/src/definition/model/properties/index.js +1 -0
  118. package/src/definition/model/properties/properties-definition-validator.js +47 -0
  119. package/src/definition/model/properties/properties-definition-validator.spec.js +52 -0
  120. package/src/definition/model/properties/property-transformer/builtin/index.d.ts +1 -0
  121. package/src/definition/model/properties/property-transformer/builtin/index.js +1 -0
  122. package/src/definition/model/properties/property-transformer/builtin/trim-transformer.d.ts +6 -0
  123. package/src/definition/model/properties/property-transformer/builtin/trim-transformer.js +19 -0
  124. package/src/definition/model/properties/property-transformer/builtin/trim-transformer.spec.js +39 -0
  125. package/src/definition/model/properties/property-transformer/index.d.ts +2 -0
  126. package/src/definition/model/properties/property-transformer/index.js +2 -0
  127. package/src/definition/model/properties/property-transformer/property-transformer-registry.d.ts +29 -0
  128. package/src/definition/model/properties/property-transformer/property-transformer-registry.js +72 -0
  129. package/src/definition/model/properties/property-transformer/property-transformer-registry.spec.js +129 -0
  130. package/src/definition/model/properties/property-transformer/property-transformer.d.ts +25 -0
  131. package/src/definition/model/properties/property-transformer/property-transformer.js +1 -0
  132. package/src/definition/model/properties/property-validator/builtin/max-length-validator.js +1 -1
  133. package/src/definition/model/properties/property-validator/builtin/max-length-validator.spec.js +10 -3
  134. package/src/definition/model/properties/property-validator/builtin/min-length-validator.js +1 -1
  135. package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js +10 -3
  136. package/src/definition/model/properties/property-validator/builtin/regexp-validator.js +1 -1
  137. package/src/definition/model/properties/property-validator/builtin/regexp-validator.spec.js +10 -3
  138. package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +51 -39
  139. 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 {Promise<void>}
19
+ * @returns {undefined}
20
20
  */
21
- async validate(modelName, modelData, isPartial = false) {
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
- for (const propName of propNames) {
33
+ propNames.forEach(propName => {
34
34
  const propDef = propDefs[propName];
35
- if (!propDef) continue;
36
- await this._validatePropertyValue(
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 {Promise<void>}
52
+ * @returns {undefined}
53
53
  */
54
- async _validatePropertyValue(modelName, propName, propDef, propValue) {
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
- // Property type.
68
- await this._validateByPropertyType(modelName, propName, propDef, propValue);
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 {Promise<void>}
86
+ * @returns {undefined}
87
87
  */
88
- async _validateByPropertyType(
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
- const arrayItemsValidationPromises = propValue.map(async value =>
137
- this._validateByPropertyType(
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
- await this.validate(propDef.model, propValue);
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 {Promise<void>}
162
+ * @returns {undefined}
164
163
  */
165
- async _validateByPropertyValidators(modelName, propName, propDef, propValue) {
164
+ _validateValueByPropertyValidators(modelName, propName, propDef, propValue) {
166
165
  if (typeof propDef === 'string' || propDef.validate == null) return;
167
- const options = propDef.validate;
168
- const propertyValidatorRegistry = this.getService(
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 container = this.container;
181
- const validateBy = async (validatorName, validatorOptions = undefined) => {
182
- const validator = propertyValidatorRegistry.getValidator(validatorName);
183
- const context = {validatorName, modelName, propName, propDef, container};
184
- const valid = await validator(propValue, validatorOptions, context);
185
- if (valid !== true) throw createError(validatorName);
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 (options && typeof options === 'string') {
188
- await validateBy(options);
189
- } else if (Array.isArray(options)) {
190
- const validationPromises = options.map(validatorName =>
191
- validateBy(validatorName),
192
- );
193
- await Promise.all(validationPromises);
194
- } else if (options !== null && typeof options === 'object') {
195
- const validationPromises = [];
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
- options,
209
+ validateDef,
212
210
  );
213
211
  }
214
212
  }