From b1b490818acb3344619f9d8c64cd1b73a0efbdb7 Mon Sep 17 00:00:00 2001 From: Matt Travers Date: Sat, 23 Apr 2022 22:19:19 -0400 Subject: [PATCH] Fixed linting and some empty array logic --- fecfile_validate_js/src/index.ts | 21 +++++++++++---------- fecfile_validate_js/tests/candidate.test.ts | 12 ++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/fecfile_validate_js/src/index.ts b/fecfile_validate_js/src/index.ts index 5efdc054..909ed122 100644 --- a/fecfile_validate_js/src/index.ts +++ b/fecfile_validate_js/src/index.ts @@ -35,17 +35,14 @@ const ajv = new Ajv({ allErrors: true, strictSchema: false }); * @returns {ValidationError[]} Modified version of Ajv output, empty array if no errors found */ export function validate(schema: any, data: any, fieldsToValidate: string[] = []): ValidationError[] { - const theSchemaUrl = schema['$schema']; - schema['$schema'] = theSchemaUrl.replace('https', 'http'); - const validator: ValidateFunction = ajv.compile(schema); const isValid: boolean = validator(data); const errors: ValidationError[] = []; - if (!isValid && !!validator.errors) { + if (!isValid && !!validator.errors?.length) { validator.errors.forEach((error) => { - const parsedError = parseError(error) - if (!fieldsToValidate.length || fieldsToValidate.includes(parsedError.path)){ + const parsedError = parseError(error); + if (!fieldsToValidate.length || fieldsToValidate.includes(parsedError.path)) { errors.push(parsedError); } }); @@ -54,9 +51,14 @@ export function validate(schema: any, data: any, fieldsToValidate: string[] = [] return errors; } +/** + * Format error message from Ajv into our ValidationError interface + * @param {ErrorObject} error - Ajv ErrorObject interface + * @returns {ValidationError} + */ function parseError(error: ErrorObject): ValidationError { - let path = error.instancePath.substring(1) - if (error.keyword == "required") { + let path = error.instancePath.substring(1); + if (error.keyword == 'required') { path = error.params.missingProperty; } return { @@ -64,6 +66,5 @@ function parseError(error: ErrorObject): ValidationError { keyword: error.keyword, params: error.params, message: !!error.message ? error.message : null, - } + }; } - diff --git a/fecfile_validate_js/tests/candidate.test.ts b/fecfile_validate_js/tests/candidate.test.ts index 112f95d2..63e91aa7 100644 --- a/fecfile_validate_js/tests/candidate.test.ts +++ b/fecfile_validate_js/tests/candidate.test.ts @@ -70,6 +70,18 @@ Deno.test({ }, }); +Deno.test({ + name: 'it should fail with for candidate office S with an invalid candidate state format', + fn: () => { + const testData = { ...data }; + testData.candidate_office = 'S'; + testData.candidate_state = 'M1'; + const result = validate(candidateContactSchema, testData); + assertEquals(result[0].path, 'candidate_state'); + assertEquals(result[0].message, 'must match pattern "^[A-Z]{2}$"'); + }, +}); + Deno.test({ name: 'it should pass with for candidate office S with a candidate state', fn: () => {