|
@@ -242,6 +242,27 @@ describe('DataParser', function () {
|
|
|
throwable(undefined)();
|
|
throwable(undefined)();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ it('should require the "keepUnknownProperties" argument to be a boolean', function () {
|
|
|
|
|
+ const S = new DataParser();
|
|
|
|
|
+ const throwable = v => () => S.parse(10, {}, {keepUnknownProperties: v});
|
|
|
|
|
+ const error = s =>
|
|
|
|
|
+ format(
|
|
|
|
|
+ 'Option "keepUnknownProperties" 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'));
|
|
|
|
|
+ expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
|
|
|
+ throwable(true)();
|
|
|
|
|
+ throwable(false)();
|
|
|
|
|
+ throwable(undefined)();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
it('should validate a given schema in the shallow mode', function () {
|
|
it('should validate a given schema in the shallow mode', function () {
|
|
|
const S = new DataParser();
|
|
const S = new DataParser();
|
|
|
const throwable = () => S.parse(10, {required: 10});
|
|
const throwable = () => S.parse(10, {required: 10});
|
|
@@ -506,7 +527,23 @@ describe('DataParser', function () {
|
|
|
expect(res).to.be.eql({p1: 'abc', p2: 'bbc', p3: 'cbc'});
|
|
expect(res).to.be.eql({p1: 'abc', p2: 'bbc', p3: 'cbc'});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('should not parse object properties without a specified schema', function () {
|
|
|
|
|
|
|
+ it('should not exclude properties when no properties schema is specified', function () {
|
|
|
|
|
+ const S = new DataParser();
|
|
|
|
|
+ const value = {p1: 1, p2: 2, p3: 3};
|
|
|
|
|
+ const schema = {type: DataType.OBJECT};
|
|
|
|
|
+ const expectedCalls = [[value, schema, undefined, S.container]];
|
|
|
|
|
+ const calls = [];
|
|
|
|
|
+ const parser = (...args) => {
|
|
|
|
|
+ calls.push(args);
|
|
|
|
|
+ return args[0];
|
|
|
|
|
+ };
|
|
|
|
|
+ S.setParsers([parser]);
|
|
|
|
|
+ const res = S.parse(value, schema);
|
|
|
|
|
+ expect(res).to.be.eql(value);
|
|
|
|
|
+ expect(expectedCalls).to.be.eql(calls);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should exclude object properties without a specified schema', function () {
|
|
|
const S = new DataParser();
|
|
const S = new DataParser();
|
|
|
const value = {p1: 1, p2: 2, p3: 3};
|
|
const value = {p1: 1, p2: 2, p3: 3};
|
|
|
const schema = {
|
|
const schema = {
|
|
@@ -526,6 +563,53 @@ describe('DataParser', function () {
|
|
|
};
|
|
};
|
|
|
S.setParsers([parser]);
|
|
S.setParsers([parser]);
|
|
|
const res = S.parse(value, schema);
|
|
const res = S.parse(value, schema);
|
|
|
|
|
+ expect(res).to.be.eql({p1: 1});
|
|
|
|
|
+ expect(expectedCalls).to.be.eql(calls);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should exclude properties when a properties schema is an empty object', function () {
|
|
|
|
|
+ const S = new DataParser();
|
|
|
|
|
+ const value = {p1: 1, p2: 2, p3: 3};
|
|
|
|
|
+ const schema = {
|
|
|
|
|
+ type: DataType.OBJECT,
|
|
|
|
|
+ properties: {},
|
|
|
|
|
+ };
|
|
|
|
|
+ const expectedCalls = [[value, schema, undefined, S.container]];
|
|
|
|
|
+ const calls = [];
|
|
|
|
|
+ const parser = (...args) => {
|
|
|
|
|
+ calls.push(args);
|
|
|
|
|
+ return args[0];
|
|
|
|
|
+ };
|
|
|
|
|
+ S.setParsers([parser]);
|
|
|
|
|
+ const res = S.parse(value, schema);
|
|
|
|
|
+ expect(res).to.be.eql({});
|
|
|
|
|
+ expect(expectedCalls).to.be.eql(calls);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should keep unknown properties when the "keepUnknownProperties" is true', function () {
|
|
|
|
|
+ const S = new DataParser();
|
|
|
|
|
+ const value = {p1: 1, p2: 2, p3: 3};
|
|
|
|
|
+ const options = {keepUnknownProperties: true};
|
|
|
|
|
+ const schema = {
|
|
|
|
|
+ type: DataType.OBJECT,
|
|
|
|
|
+ properties: {p1: {type: DataType.NUMBER}},
|
|
|
|
|
+ };
|
|
|
|
|
+ const expectedCalls = [
|
|
|
|
|
+ [value, schema, options, S.container],
|
|
|
|
|
+ [
|
|
|
|
|
+ value.p1,
|
|
|
|
|
+ schema.properties.p1,
|
|
|
|
|
+ {...options, sourcePath: 'p1'},
|
|
|
|
|
+ S.container,
|
|
|
|
|
+ ],
|
|
|
|
|
+ ];
|
|
|
|
|
+ const calls = [];
|
|
|
|
|
+ const parser = (...args) => {
|
|
|
|
|
+ calls.push(args);
|
|
|
|
|
+ return args[0];
|
|
|
|
|
+ };
|
|
|
|
|
+ S.setParsers([parser]);
|
|
|
|
|
+ const res = S.parse(value, schema, options);
|
|
|
expect(res).to.be.eql(value);
|
|
expect(res).to.be.eql(value);
|
|
|
expect(expectedCalls).to.be.eql(calls);
|
|
expect(expectedCalls).to.be.eql(calls);
|
|
|
});
|
|
});
|