Skip to content

Commit

Permalink
Fix validateCircuitInputTranslationSchema return type to be a single …
Browse files Browse the repository at this point in the history
…string representing error
  • Loading branch information
ognjenkurtic committed Jun 19, 2024
1 parent 51da54a commit 9295224
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export class WorkstepAgent {
}

public throwIfCircuitInputTranslationSchemaInvalid(schema): void {
const [valid, error] =
const error =
this.cips.validateCircuitInputTranslationSchema(schema);
if (!valid) {
if (error) {
throw new BadRequestException(error);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ beforeAll(async () => {
});

describe('validateCircuitInputTranslationSchema', () => {
it('Should return [false, "Missing mapping array"] if mapping array is missing', () => {
it('Should return "Missing mapping array" if mapping array is missing', () => {
// Arrange
const schema = '{}';

// Act
const result = cips.validateCircuitInputTranslationSchema(schema);

// Assert
expect(result).toEqual([false, 'Missing mapping array']);
expect(result).toEqual('Missing mapping array');
});

it('Should return [false, "{circuitInput}"] if any required property has incorrect type', () => {
it('Should return "{circuitInput}" if any required property has incorrect type', () => {
// Arrange
const schema =
'{"mapping": [{"circuitInput": 123, "description": "desc1", "payloadJsonPath": "path1", "dataType": "string"}]}';
Expand All @@ -31,10 +31,10 @@ describe('validateCircuitInputTranslationSchema', () => {
const result = cips.validateCircuitInputTranslationSchema(schema);

// Assert
expect(result).toEqual([false, '123 not of type string']);
expect(result).toEqual('123 not of type string');
});

it('Should return [false, "{description}"] if any required property has incorrect type', () => {
it('Should return "{description}" if any required property has incorrect type', () => {
// Arrange
const schema =
'{"mapping": [{"circuitInput": "123", "description": false, "payloadJsonPath": "path1", "dataType": "string"}]}';
Expand All @@ -43,10 +43,10 @@ describe('validateCircuitInputTranslationSchema', () => {
const result = cips.validateCircuitInputTranslationSchema(schema);

// Assert
expect(result).toEqual([false, 'false not of type string']);
expect(result).toEqual('false not of type string');
});

it('Should return [false, "{payloadJsonPath}"] if any required property has incorrect type', () => {
it('Should return "{payloadJsonPath}" if any required property has incorrect type', () => {
// Arrange
const schema =
'{"mapping": [{"circuitInput": "123", "description": "desc", "payloadJsonPath": 12, "dataType": "string"}]}';
Expand All @@ -55,10 +55,10 @@ describe('validateCircuitInputTranslationSchema', () => {
const result = cips.validateCircuitInputTranslationSchema(schema);

// Assert
expect(result).toEqual([false, '12 not of type string']);
expect(result).toEqual('12 not of type string');
});

it('Should return [false, "{dataType}"] if any required property has incorrect type', () => {
it('Should return "{dataType}" if any required property has incorrect type', () => {
// Arrange
const schema =
'{"mapping": [{"circuitInput": "123", "description": "desc", "payloadJsonPath": "path1", "dataType": 232}]}';
Expand All @@ -67,10 +67,10 @@ describe('validateCircuitInputTranslationSchema', () => {
const result = cips.validateCircuitInputTranslationSchema(schema);

// Assert
expect(result).toEqual([false, '232 not of type string']);
expect(result).toEqual('232 not of type string');
});

it('Should return [false, "arrayType not defined properly for {circuitInput}"] if dataType is array and arrayType is not defined properly', () => {
it('Should return "arrayType not defined properly for {circuitInput}" if dataType is array and arrayType is not defined properly', () => {
// Arrange
const schema =
'{"mapping": [{"circuitInput": "input1", "description": "desc1", "payloadJsonPath": "path1", "dataType": "array", "arrayType": 123}]}';
Expand All @@ -85,7 +85,7 @@ describe('validateCircuitInputTranslationSchema', () => {
]);
});

it('Should return [false, "defaultValue not of type {dataType} for {circuitInput}"] if defaultValue type does not match dataType', () => {
it('Should return "defaultValue not of type {dataType} for {circuitInput}" if defaultValue type does not match dataType', () => {
// Arrange
const schema =
'{"mapping": [{"circuitInput": "input1", "description": "desc1", "payloadJsonPath": "path1", "dataType": "string", "defaultValue": 123}]}';
Expand All @@ -100,7 +100,7 @@ describe('validateCircuitInputTranslationSchema', () => {
]);
});

it('Should return [false, error] if an error occurs during schema parsing', () => {
it('Should return error] if an error occurs during schema parsing', () => {
// Arrange
const schema = 'invalid JSON';

Expand All @@ -114,7 +114,7 @@ describe('validateCircuitInputTranslationSchema', () => {
]);
});

it('Should return [true, ""] if basic valid schema passed in', () => {
it('Should return "" if basic valid schema passed in', () => {
// Arrange
const schema =
'{"mapping": [{"circuitInput": "input1", "description": "desc1", "payloadJsonPath": "path1", "dataType": "string"}]}';
Expand All @@ -123,10 +123,10 @@ describe('validateCircuitInputTranslationSchema', () => {
const result = cips.validateCircuitInputTranslationSchema(schema);

// Assert
expect(result).toEqual([true, '']);
expect(result).toEqual('');
});

it('Should return [true, ""] if valid schema with default value passed in', () => {
it('Should return "" if valid schema with default value passed in', () => {
// Arrange
const schema =
'{"mapping": [{"circuitInput": "input1", "description": "desc1", "payloadJsonPath": "path1", "dataType": "string", "defaultValue": "123"}]}';
Expand All @@ -135,10 +135,10 @@ describe('validateCircuitInputTranslationSchema', () => {
const result = cips.validateCircuitInputTranslationSchema(schema);

// Assert
expect(result).toEqual([true, '']);
expect(result).toEqual('');
});

it('Should return [true, ""] if valid schema with arrayType in', () => {
it('Should return "" if valid schema with arrayType in', () => {
// Arrange
const schema =
'{"mapping": [{"circuitInput": "input1", "description": "desc1", "payloadJsonPath": "path1", "dataType": "array", "arrayType": "string"}]}';
Expand All @@ -147,7 +147,7 @@ describe('validateCircuitInputTranslationSchema', () => {
const result = cips.validateCircuitInputTranslationSchema(schema);

// Assert
expect(result).toEqual([true, '']);
expect(result).toEqual('');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,48 @@ export class CircuitInputsParserService {

public validateCircuitInputTranslationSchema(
schema: string,
): [boolean, string] {
): string {
try {
const parsedData: CircuitInputsMapping = JSON.parse(schema);

if (!parsedData.mapping || !Array.isArray(parsedData.mapping)) {
return [false, `Missing mapping array`];
return `Missing mapping array`;
}

for (const mapping of parsedData.mapping) {
if (typeof mapping.circuitInput !== 'string') {
return [false, `${mapping.circuitInput} not of type string`];
return `${mapping.circuitInput} not of type string`;
}

if (typeof mapping.description !== 'string') {
return [false, `${mapping.description} not of type string`];
return `${mapping.description} not of type string`;
}

if (typeof mapping.payloadJsonPath !== 'string') {
return [false, `${mapping.payloadJsonPath} not of type string`];
return `${mapping.payloadJsonPath} not of type string`;
}

if (typeof mapping.dataType !== 'string') {
return [false, `${mapping.dataType} not of type string`];
return `${mapping.dataType} not of type string`;
}

if (mapping.dataType === 'array') {
if (!mapping.arrayType || typeof mapping.arrayType !== 'string') {
return [
false,
`arrayType not defined properly for ${mapping.circuitInput}`,
];
return `arrayType not defined properly for ${mapping.circuitInput}`;
}
}

if (
mapping.defaultValue &&
typeof mapping.defaultValue !== mapping.dataType
) {
return [
false,
`defaultValue not of type ${mapping.dataType} for ${mapping.circuitInput}`,
];
return `defaultValue not of type ${mapping.dataType} for ${mapping.circuitInput}`;
}
}

return [true, ''];
return '';
} catch (error) {
return [false, error.message];
return error.message;
}
}

Expand Down

0 comments on commit 9295224

Please sign in to comment.