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

refactor: arguments validation ordering

e22m4u 1 день назад
Родитель
Сommit
c8632381eb
3 измененных файлов с 48 добавлено и 41 удалено
  1. 6 6
      dist/cjs/index.cjs
  2. 21 14
      src/validate-projection-schema.js
  3. 21 21
      src/validate-projection-schema.spec.js

+ 6 - 6
dist/cjs/index.cjs

@@ -34,6 +34,12 @@ var import_js_format2 = require("@e22m4u/js-format");
 // src/validate-projection-schema.js
 var import_js_format = require("@e22m4u/js-format");
 function validateProjectionSchema(schema, shallowMode = false, validatedSchemas = /* @__PURE__ */ new Set()) {
+  if (!schema || typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) {
+    throw new import_js_format.InvalidArgumentError(
+      "Projection schema must be an Object, a Function or a non-empty String, but %v was given.",
+      schema
+    );
+  }
   if (typeof shallowMode !== "boolean") {
     throw new import_js_format.InvalidArgumentError(
       'Argument "shallowMode" must be a Boolean, but %v was given.',
@@ -49,12 +55,6 @@ function validateProjectionSchema(schema, shallowMode = false, validatedSchemas
   if (validatedSchemas.has(schema)) {
     return;
   }
-  if (!schema || typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) {
-    throw new import_js_format.InvalidArgumentError(
-      "Projection schema must be an Object, a Function or a non-empty String, but %v was given.",
-      schema
-    );
-  }
   if (typeof schema !== "object") {
     return;
   }

+ 21 - 14
src/validate-projection-schema.js

@@ -12,12 +12,31 @@ export function validateProjectionSchema(
   shallowMode = false,
   validatedSchemas = new Set(),
 ) {
+  // если схема не является объектом, функцией
+  // и не пустой строкой, то выбрасывается ошибка
+  if (
+    !schema ||
+    (typeof schema !== 'object' &&
+      typeof schema !== 'function' &&
+      typeof schema !== 'string') ||
+    Array.isArray(schema)
+  ) {
+    throw new InvalidArgumentError(
+      'Projection schema must be an Object, a Function ' +
+        'or a non-empty String, but %v was given.',
+      schema,
+    );
+  }
+  // если флаг поверхностного режима не является
+  // логическим значением, то выбрасывается ошибка
   if (typeof shallowMode !== 'boolean') {
     throw new InvalidArgumentError(
       'Argument "shallowMode" must be a Boolean, but %v was given.',
       shallowMode,
     );
   }
+  // если набор проверенных схем не является
+  // экземпляром Set, то выбрасывается ошибка
   if (!(validatedSchemas instanceof Set)) {
     throw new InvalidArgumentError(
       'Argument "validatedSchemas" must be ' +
@@ -30,20 +49,8 @@ export function validateProjectionSchema(
   if (validatedSchemas.has(schema)) {
     return;
   }
-  // schema
-  if (
-    !schema ||
-    (typeof schema !== 'object' &&
-      typeof schema !== 'function' &&
-      typeof schema !== 'string') ||
-    Array.isArray(schema)
-  ) {
-    throw new InvalidArgumentError(
-      'Projection schema must be an Object, a Function ' +
-        'or a non-empty String, but %v was given.',
-      schema,
-    );
-  }
+  // если схема не является объектом,
+  // то проверка пропускается
   if (typeof schema !== 'object') {
     return;
   }

+ 21 - 21
src/validate-projection-schema.spec.js

@@ -3,6 +3,27 @@ import {format} from '@e22m4u/js-format';
 import {validateProjectionSchema} from './validate-projection-schema.js';
 
 describe('validateProjectionSchema', function () {
+  it('should require the "schema" argument to be a valid value', function () {
+    const throwable = v => () => validateProjectionSchema(v);
+    const error = s =>
+      format(
+        'Projection schema must be an Object, a Function ' +
+          'or a non-empty String, but %s was given.',
+        s,
+      );
+    expect(throwable('')).to.throw(error('""'));
+    expect(throwable(10)).to.throw(error('10'));
+    expect(throwable(0)).to.throw(error('0'));
+    expect(throwable(true)).to.throw(error('true'));
+    expect(throwable(false)).to.throw(error('false'));
+    expect(throwable([])).to.throw(error('Array'));
+    expect(throwable(undefined)).to.throw(error('undefined'));
+    expect(throwable(null)).to.throw(error('null'));
+    throwable('str')();
+    throwable({})();
+    throwable(() => ({}))();
+  });
+
   it('should require the "shallowMode" argument to be a Boolean', function () {
     const throwable = v => () => validateProjectionSchema({}, v);
     const error = s =>
@@ -49,27 +70,6 @@ describe('validateProjectionSchema', function () {
     validateProjectionSchema(schema, false, validatedSchemas);
   });
 
-  it('should require the "schema" argument to be a valid value', function () {
-    const throwable = v => () => validateProjectionSchema(v);
-    const error = s =>
-      format(
-        'Projection schema must be an Object, a Function ' +
-          'or a non-empty String, but %s was given.',
-        s,
-      );
-    expect(throwable('')).to.throw(error('""'));
-    expect(throwable(10)).to.throw(error('10'));
-    expect(throwable(0)).to.throw(error('0'));
-    expect(throwable(true)).to.throw(error('true'));
-    expect(throwable(false)).to.throw(error('false'));
-    expect(throwable([])).to.throw(error('Array'));
-    expect(throwable(undefined)).to.throw(error('undefined'));
-    expect(throwable(null)).to.throw(error('null'));
-    throwable('str')();
-    throwable({})();
-    throwable(() => ({}))();
-  });
-
   it('should require property options to be an object or a boolean', function () {
     const throwable = v => () => validateProjectionSchema({foo: v});
     const error = s =>