Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(jsonpopulator): remove data values from validation error messages #483

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions packages/concerto-core/lib/serializer/jsonpopulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

'use strict';

const { TypedStack } = require('@accordproject/concerto-util');
const Relationship = require('../model/relationship');
const Util = require('../util');
const ModelUtil = require('../modelutil');
Expand Down Expand Up @@ -100,7 +101,9 @@ class JSONPopulator {
* @return {Object} the result of visiting or null
* @private
*/
visit(thing, parameters) {
visit(thing, parameters = {}) {
parameters.path ?? (parameters.path = new TypedStack('$'));

if (thing.isClassDeclaration?.()) {
return this.visitClassDeclaration(thing, parameters);
} else if (thing.isRelationship?.()) {
Expand All @@ -122,6 +125,7 @@ class JSONPopulator {
visitClassDeclaration(classDeclaration, parameters) {
const jsonObj = parameters.jsonStack.pop();
const resourceObj = parameters.resourceStack.pop();
parameters.path ?? (parameters.path = new TypedStack('$'));

const properties = getAssignableProperties(jsonObj);
validateProperties(properties, classDeclaration);
Expand All @@ -136,12 +140,13 @@ class JSONPopulator {
}
}
if (value !== null) {
parameters.path.push(`.${property}`);
parameters.jsonStack.push(value);
const classProperty = classDeclaration.getProperty(property);
resourceObj[property] = classProperty.accept(this,parameters);
parameters.path.pop();
}
});

return resourceObj;
}

Expand All @@ -153,6 +158,7 @@ class JSONPopulator {
* @private
*/
visitField(field, parameters) {
parameters.path ?? (parameters.path = new TypedStack('$'));
let jsonObj = parameters.jsonStack.pop();
let result = null;

Expand All @@ -164,8 +170,10 @@ class JSONPopulator {
}
result = [];
for(let n=0; n < jsonObj.length; n++) {
parameters.path.push(`[${n}]`);
const jsonItem = jsonObj[n];
result.push(this.convertItem(field,jsonItem, parameters));
parameters.path.pop();
}
}
else {
Expand Down Expand Up @@ -222,7 +230,7 @@ class JSONPopulator {
classDeclaration.accept(this, parameters);
}
else {
result = this.convertToObject(field,jsonItem);
result = this.convertToObject(field, jsonItem, parameters);
}

return result;
Expand All @@ -233,22 +241,25 @@ class JSONPopulator {
*
* @param {Field} field - the field declaration of the object
* @param {Object} json - the JSON object to convert to a Concerto Object
* @param {Object} parameters - the parameters
* @return {string} the text representation
*/
convertToObject(field, json) {
convertToObject(field, json, parameters = {}) {
let result = null;
parameters.path ?? (parameters.path = new TypedStack('$'));
const path = parameters.path.stack.join('');

switch(field.getType()) {
case 'DateTime': {
if (json && typeof json === 'object' && typeof json.isBefore === 'function') {
result = json;
} else if (typeof json !== 'string') {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
throw new ValidationException(`Expected value at path \`${path}\` to be of type \`${field.getType()}\``);
} else {
result = dayjs.utc(json).utcOffset(this.utcOffset);
}
if (!result.isValid()) {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
throw new ValidationException(`Expected value at path \`${path}\` to be of type \`${field.getType()}\``);
}
}
break;
Expand All @@ -257,36 +268,36 @@ class JSONPopulator {
const num = this.ergo ? json.$nat : json;
if (typeof num === 'number') {
if (Math.trunc(num) !== num) {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
throw new ValidationException(`Expected value at path \`${path}\` to be of type \`${field.getType()}\``);
} else {
result = num;
}
} else {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
throw new ValidationException(`Expected value at path \`${path}\` to be of type \`${field.getType()}\``);
}
}
break;
case 'Double': {
if (typeof json === 'number') {
result = parseFloat(json);
} else {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
throw new ValidationException(`Expected value at path \`${path}\` to be of type \`${field.getType()}\``);
}
}
break;
case 'Boolean': {
if (typeof json === 'boolean') {
result = json;
} else {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
throw new ValidationException(`Expected value at path \`${path}\` to be of type \`${field.getType()}\``);
}
}
break;
case 'String':
if (typeof json === 'string') {
result = json;
} else {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
throw new ValidationException(`Expected value at path \`${path}\` to be of type \`${field.getType()}\``);
}
break;
default: {
Expand Down Expand Up @@ -314,6 +325,7 @@ class JSONPopulator {
* @private
*/
visitRelationshipDeclaration(relationshipDeclaration, parameters) {
parameters.path ?? (parameters.path = new TypedStack('$'));
let jsonObj = parameters.jsonStack.pop();
let result = null;

Expand Down
Loading