From 9295224ffed4778ab9a650b80b273fc875000092 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Wed, 19 Jun 2024 22:15:16 +0200 Subject: [PATCH] Fix validateCircuitInputTranslationSchema return type to be a single string representing error --- .../worksteps/agents/worksteps.agent.ts | 4 +- .../circuitInputParser.service.spec.ts | 38 +++++++++---------- .../circuitInputParser.service.ts | 26 +++++-------- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/examples/bri-3/src/bri/workgroup/worksteps/agents/worksteps.agent.ts b/examples/bri-3/src/bri/workgroup/worksteps/agents/worksteps.agent.ts index c075ff609..c7bbeb186 100644 --- a/examples/bri-3/src/bri/workgroup/worksteps/agents/worksteps.agent.ts +++ b/examples/bri-3/src/bri/workgroup/worksteps/agents/worksteps.agent.ts @@ -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); } } diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index 597e27add..abf7b1ea6 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -11,7 +11,7 @@ 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 = '{}'; @@ -19,10 +19,10 @@ describe('validateCircuitInputTranslationSchema', () => { 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"}]}'; @@ -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"}]}'; @@ -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"}]}'; @@ -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}]}'; @@ -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}]}'; @@ -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}]}'; @@ -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'; @@ -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"}]}'; @@ -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"}]}'; @@ -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"}]}'; @@ -147,7 +147,7 @@ describe('validateCircuitInputTranslationSchema', () => { const result = cips.validateCircuitInputTranslationSchema(schema); // Assert - expect(result).toEqual([true, '']); + expect(result).toEqual(''); }); }); diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts index 7140d0f56..8fd8b76a1 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -7,37 +7,34 @@ 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}`; } } @@ -45,16 +42,13 @@ export class CircuitInputsParserService { 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; } }