Просмотр исходного кода

chore: allows to use the option "model" in property definition of Array

e22m4u 2 лет назад
Родитель
Сommit
922a72c9b2

+ 26 - 8
src/definition/model/properties/properties-definition-validator.js

@@ -1,5 +1,6 @@
 import {Service} from '@e22m4u/js-service';
 import {DataType as Type} from './data-type.js';
+import {capitalize} from '../../../utils/index.js';
 import {InvalidArgumentError} from '../../../errors/index.js';
 import {PrimaryKeysDefinitionValidator} from './primary-keys-definition-validator.js';
 
@@ -177,13 +178,30 @@ export class PropertiesDefinitionValidator extends Service {
         modelName,
         propDef.type,
       );
-    if (propDef.model && propDef.type !== Type.OBJECT)
-      throw new InvalidArgumentError(
-        'The property %v of the model %v has the non-object type, ' +
-          'so it should not have the option "model" to be provided.',
-        propName,
-        modelName,
-        propDef.type,
-      );
+    if (
+      propDef.model &&
+      propDef.type !== Type.OBJECT &&
+      propDef.itemType !== Type.OBJECT
+    ) {
+      if (propDef.type !== Type.ARRAY) {
+        throw new InvalidArgumentError(
+          'The option "model" is not supported for %s property type, ' +
+            'so the property %v of the model %v should not have ' +
+            'the option "model" to be provided.',
+          capitalize(propDef.type),
+          propName,
+          modelName,
+        );
+      } else {
+        throw new InvalidArgumentError(
+          'The option "model" is not supported for Array property type of %s, ' +
+            'so the property %v of the model %v should not have ' +
+            'the option "model" to be provided.',
+          capitalize(propDef.itemType),
+          propName,
+          modelName,
+        );
+      }
+    }
   }
 }

+ 37 - 10
src/definition/model/properties/properties-definition-validator.spec.js

@@ -332,7 +332,7 @@ describe('PropertiesDefinitionValidator', function () {
       validate(DataType.ARRAY);
     });
 
-    it('expects a non-object property should not have the option "model" to be provided', function () {
+    it('the option "model" requires the "object" property type', function () {
       const validate = v => () => {
         const foo = {
           type: v,
@@ -340,15 +340,42 @@ describe('PropertiesDefinitionValidator', function () {
         };
         S.validate('model', {foo});
       };
-      const error =
-        'The property "foo" of the model "model" has the non-object type, ' +
-        'so it should not have the option "model" to be provided.';
-      expect(validate(DataType.ANY)).to.throw(error);
-      expect(validate(DataType.STRING)).to.throw(error);
-      expect(validate(DataType.NUMBER)).to.throw(error);
-      expect(validate(DataType.BOOLEAN)).to.throw(error);
-      expect(validate(DataType.ARRAY)).to.throw(error);
-      validate(DataType.OBJECT);
+      const error = v =>
+        format(
+          'The option "model" is not supported for %s property type, ' +
+            'so the property "foo" of the model "model" should not have ' +
+            'the option "model" to be provided.',
+          v,
+        );
+      expect(validate(DataType.ANY)).to.throw(error('Any'));
+      expect(validate(DataType.STRING)).to.throw(error('String'));
+      expect(validate(DataType.NUMBER)).to.throw(error('Number'));
+      expect(validate(DataType.BOOLEAN)).to.throw(error('Boolean'));
+      validate(DataType.OBJECT)();
+    });
+
+    it('the option "model" requires the "object" item type', function () {
+      const validate = v => () => {
+        const foo = {
+          type: DataType.ARRAY,
+          itemType: v,
+          model: 'model',
+        };
+        S.validate('model', {foo});
+      };
+      const error = v =>
+        format(
+          'The option "model" is not supported for Array property type of %s, ' +
+            'so the property "foo" of the model "model" should not have ' +
+            'the option "model" to be provided.',
+          v,
+        );
+      expect(validate(DataType.ANY)).to.throw(error('Any'));
+      expect(validate(DataType.STRING)).to.throw(error('String'));
+      expect(validate(DataType.NUMBER)).to.throw(error('Number'));
+      expect(validate(DataType.BOOLEAN)).to.throw(error('Boolean'));
+      expect(validate(DataType.ARRAY)).to.throw(error('Array'));
+      validate(DataType.OBJECT)();
     });
 
     it('uses PrimaryKeysDefinitionValidator to validate primary keys', function () {