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

fix: passing "sourcePath" option to the validator

e22m4u 2 недель назад
Родитель
Сommit
8fbd0e7a33
3 измененных файлов с 38 добавлено и 3 удалено
  1. 2 2
      dist/cjs/index.cjs
  2. 1 1
      src/data-parser.js
  3. 35 0
      src/data-parser.spec.js

+ 2 - 2
dist/cjs/index.cjs

@@ -896,7 +896,7 @@ function defaultValueSetter(value, schema, options, container) {
   if (typeof schema.default === "function") {
     return schema.default(container);
   }
-  return structuredClone(schema.default);
+  return schema.default && typeof schema.default === "object" ? structuredClone(schema.default) : schema.default;
 }
 __name(defaultValueSetter, "defaultValueSetter");
 
@@ -1076,7 +1076,7 @@ var DataParser = class extends import_js_service16.Service {
     }
     if (!noParsingErrors) {
       const validator = this.getService(DataValidator);
-      validator.validate(value, schema, { shallowMode: true });
+      validator.validate(value, schema, { shallowMode: true, sourcePath });
     }
     return value;
   }

+ 1 - 1
src/data-parser.js

@@ -236,7 +236,7 @@ export class DataParser extends Service {
     // значение проверяется согласно схеме данных
     if (!noParsingErrors) {
       const validator = this.getService(DataValidator);
-      validator.validate(value, schema, {shallowMode: true});
+      validator.validate(value, schema, {shallowMode: true, sourcePath});
     }
     return value;
   }

+ 35 - 0
src/data-parser.spec.js

@@ -2,6 +2,7 @@ import {expect} from 'chai';
 import {DataType} from './data-type.js';
 import {format} from '@e22m4u/js-format';
 import {DataParser} from './data-parser.js';
+import {DataValidationError} from './errors/index.js';
 import {DataSchemaRegistry} from './data-schema-registry.js';
 
 import {
@@ -12,6 +13,7 @@ import {
   booleanTypeParser,
   defaultValueSetter,
 } from './data-parsers/index.js';
+import {DataValidator} from './data-validator.js';
 
 describe('DataParser', function () {
   describe('getParsers', function () {
@@ -667,5 +669,38 @@ describe('DataParser', function () {
       const res = S.parse(value, schema);
       expect(res).to.be.eql(value);
     });
+
+    it('should validate a parsing result in the shallow mode', function () {
+      const S = new DataParser();
+      const value = 10;
+      const schema = {type: DataType.NUMBER};
+      const parser = value => value;
+      S.setParsers([parser]);
+      const validator = S.getService(DataValidator);
+      validator.validate = (...args) => {
+        expect(args[0]).to.be.eq(value);
+        expect(args[1]).to.be.eql(schema);
+        expect(args[2]).to.be.eql({shallowMode: true, sourcePath: undefined});
+        throw new DataValidationError('Validation error');
+      };
+      const throwable = () => S.parse(value, schema);
+      expect(throwable).to.throw('Validation error');
+    });
+
+    it('should pass a source path to the validator options', function () {
+      const S = new DataParser();
+      const sourcePath = 'mySource';
+      const parser = value => value;
+      S.setParsers([parser]);
+      const validator = S.getService(DataValidator);
+      let invoked = 0;
+      validator.validate = (...args) => {
+        expect(args[2]).to.be.an('object');
+        expect(args[2]).to.have.property('sourcePath', sourcePath);
+        invoked++;
+      };
+      S.parse(10, {type: DataType.NUMBER}, {sourcePath});
+      expect(invoked).to.be.eq(1);
+    });
   });
 });