|
|
@@ -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);
|
|
|
+ });
|
|
|
});
|
|
|
});
|