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

chore: improve types and tests

e22m4u 1 неделя назад
Родитель
Сommit
1204ba5166

+ 1 - 1
dist/cjs/index.cjs

@@ -42,7 +42,7 @@ function validateProjectionSchema(schema, shallowMode = false) {
   }
   if (typeof shallowMode !== "boolean") {
     throw new import_js_format.InvalidArgumentError(
-      'The parameter "shallowMode" should be a Boolean, but %v was given.',
+      'Parameter "shallowMode" must be a Boolean, but %v was given.',
       shallowMode
     );
   }

+ 2 - 0
src/data-projector.spec.js

@@ -142,6 +142,7 @@ describe('DataProjector', function () {
 
     it('should throw an error when the resolver option is provided', function () {
       const S = new DataProjector();
+      // @ts-ignore
       const throwable = v => () => S.project({}, {}, {resolver: v});
       const error = 'Option "resolver" is not supported for the DataProjector.';
       expect(throwable('str')).to.throw(error);
@@ -158,6 +159,7 @@ describe('DataProjector', function () {
 
     it('should validate the given schema object', function () {
       const S = new DataProjector();
+      // @ts-ignore
       const throwable = () => S.project({foo: 10}, {foo: 'bar'});
       expect(throwable).to.throw(
         'Property options must be a Boolean or an Object, but 10 was given.',

+ 1 - 1
src/projection-schema-registry.spec.js

@@ -51,7 +51,7 @@ describe('ProjectionSchemaRegistry', function () {
       const S = new ProjectionSchemaRegistry();
       const schema = {foo: true, bar: false};
       S.defineSchema('mySchema', schema);
-      expect(S._schemas.get('mySchema')).to.be.eql(schema);
+      expect(S['_schemas'].get('mySchema')).to.be.eql(schema);
     });
 
     it('should return this', function () {

+ 2 - 0
src/validate-projection-schema.d.ts

@@ -4,7 +4,9 @@ import {ProjectionSchema} from './projection-schema.js';
  * Validate projection schema.
  *
  * @param schema
+ * @param shallowMode
  */
 export declare function validateProjectionSchema(
   schema: ProjectionSchema,
+  shallowMode?: boolean
 ): void;

+ 1 - 1
src/validate-projection-schema.js

@@ -18,7 +18,7 @@ export function validateProjectionSchema(schema, shallowMode = false) {
   // shallowMode
   if (typeof shallowMode !== 'boolean') {
     throw new InvalidArgumentError(
-      'The parameter "shallowMode" should be a Boolean, but %v was given.',
+      'Parameter "shallowMode" must be a Boolean, but %v was given.',
       shallowMode,
     );
   }

+ 30 - 1
src/validate-projection-schema.spec.js

@@ -3,7 +3,7 @@ import {format} from '@e22m4u/js-format';
 import {validateProjectionSchema} from './validate-projection-schema.js';
 
 describe('validateProjectionSchema', function () {
-  it('should require an object', function () {
+  it('should require the schema argument to be a object', function () {
     const throwable = v => () => validateProjectionSchema(v);
     const error = s =>
       format('Projection schema must be an Object, but %s was given.', s);
@@ -19,6 +19,22 @@ describe('validateProjectionSchema', function () {
     throwable({})();
   });
 
+  it('should require the shallowMode parameter to be a boolean', function () {
+    const throwable = v => () => validateProjectionSchema({}, v);
+    const error = s =>
+      format('Parameter "shallowMode" must be a Boolean, but %s was given.', s);
+    expect(throwable('str')).to.throw(error('"str"'));
+    expect(throwable('')).to.throw(error('""'));
+    expect(throwable(10)).to.throw(error('10'));
+    expect(throwable(0)).to.throw(error('0'));
+    expect(throwable([])).to.throw(error('Array'));
+    expect(throwable({})).to.throw(error('Object'));
+    expect(throwable(null)).to.throw(error('null'));
+    throwable(true)();
+    throwable(false)();
+    throwable(undefined)();
+  });
+
   it('should require schema properties to be a boolean or an object', function () {
     const throwable = v => () => validateProjectionSchema({foo: v});
     const error = s =>
@@ -158,4 +174,17 @@ describe('validateProjectionSchema', function () {
     validateProjectionSchema({foo: {schema: 'mySchema'}});
     validateProjectionSchema({foo: {schema: {bar: {schema: 'mySchema'}}}});
   });
+
+  it('should validate root schema in shallow mode', function () {
+    // @ts-ignore
+    const throwable = () => validateProjectionSchema({foo: 10}, true);
+    expect(throwable).to.throw(
+      'Property options must be a Boolean or an Object, but 10 was given.',
+    );
+  });
+
+  it('should skip nested schema checking in shallow mode', function () {
+    // @ts-ignore
+    validateProjectionSchema({foo: {schema: {prop: 10}}}, true);
+  });
 });

+ 17 - 3
tsconfig.json

@@ -1,9 +1,23 @@
 {
   "compilerOptions": {
-    "rootDir": "src",
     "noEmit": true,
     "target": "es2022",
     "module": "NodeNext",
-    "moduleResolution": "NodeNext"
-  }
+    "moduleResolution": "NodeNext",
+    "allowJs": true,
+    "checkJs": true,
+
+    "strict": false,
+    "noImplicitAny": false,
+    "skipLibCheck": false,
+    "strictNullChecks": true,
+  },
+  "include": [
+    "./src/**/*.ts",
+    "./src/**/*.js"
+  ],
+  "exclude": [
+    "node_modules",
+    "dist"
+  ]
 }