@e22m4u/js-repository 0.1.5 → 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.
@@ -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,9 @@ 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
55
  // undefined and null
56
56
  if (propValue == null) {
57
57
  const isRequired =
@@ -65,9 +65,9 @@ export class ModelDataValidator extends Service {
65
65
  );
66
66
  }
67
67
  // Property type.
68
- await this._validateByPropertyType(modelName, propName, propDef, propValue);
68
+ this._validateValueByPropertyType(modelName, propName, propDef, propValue);
69
69
  // Property validators.
70
- await this._validateByPropertyValidators(
70
+ this._validateValueByPropertyValidators(
71
71
  modelName,
72
72
  propName,
73
73
  propDef,
@@ -76,16 +76,16 @@ export class ModelDataValidator extends Service {
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,29 +142,28 @@ 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;
166
+ const validateDef = propDef.validate;
168
167
  const propertyValidatorRegistry = this.getService(
169
168
  PropertyValidatorRegistry,
170
169
  );
@@ -177,30 +176,31 @@ export class ModelDataValidator extends Service {
177
176
  propValue,
178
177
  validatorName,
179
178
  );
180
- const container = this.container;
181
- const validateBy = async (validatorName, validatorOptions = undefined) => {
179
+ const validateBy = (validatorName, validatorOptions = undefined) => {
182
180
  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);
181
+ const context = {validatorName, modelName, propName};
182
+ const valid = validator(propValue, validatorOptions, context);
183
+ if (valid instanceof Promise) {
184
+ throw new InvalidArgumentError(
185
+ 'Asynchronous property validators are not supported, ' +
186
+ 'but the property validator %v returns a Promise.',
187
+ validatorName,
188
+ );
189
+ } else if (valid !== true) {
190
+ throw createError(validatorName);
191
+ }
186
192
  };
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);
193
+ if (validateDef && typeof validateDef === 'string') {
194
+ validateBy(validateDef);
195
+ } else if (Array.isArray(validateDef)) {
196
+ validateDef.forEach(validatorName => validateBy(validatorName));
197
+ } else if (validateDef !== null && typeof validateDef === 'object') {
198
+ Object.keys(validateDef).forEach(validatorName => {
199
+ if (Object.prototype.hasOwnProperty.call(validateDef, validatorName)) {
200
+ const validatorOptions = validateDef[validatorName];
201
+ validateBy(validatorName, validatorOptions);
201
202
  }
202
203
  });
203
- await Promise.all(validationPromises);
204
204
  } else {
205
205
  throw new InvalidArgumentError(
206
206
  'The provided option "validate" of the property %v in the model %v ' +
@@ -208,7 +208,7 @@ export class ModelDataValidator extends Service {
208
208
  'but %v given.',
209
209
  propName,
210
210
  modelName,
211
- options,
211
+ validateDef,
212
212
  );
213
213
  }
214
214
  }