Skip to content

Commit

Permalink
61 individual record validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-snodgrass committed Jan 7, 2024
1 parent 41ebe61 commit 7dc603d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 103 deletions.
109 changes: 14 additions & 95 deletions api/src/lib/workbookValidation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'fs'

import { ValidationError } from 'src/lib/validation-error'
import { getRules } from 'src/lib/validation-rules'
import {
Expand All @@ -12,45 +10,6 @@ import {
const translateRecords = (records: WorkbookRecord[], type: string | number) =>
records.filter((rec) => rec.type === type).map((r) => r.content)
const rules = getRules()
const realRecords = fs.readFileSync(`${__dirname}/cpf_uploadFile.json`, 'utf-8')
// console.log(workbook)
describe('the real example', () => {
let records
beforeEach(() => {
records = JSON.parse(realRecords)
})
describe('validateVersion', () => {
it('should not return an error', () => {
expect(validateVersion({ rules, records })).toBeUndefined()
})
})
describe('validateProjectUseCode', () => {
it('should not return an error', () => {
expect(validateProjectUseCode({ records })).toBeUndefined()
})
})
describe('validateRecord', () => {
it('should return empty array for logic rules', () => {
expect(
validateRecord({
upload: {},
record: translateRecords(records, 'logic')[0],
typeRules: rules['logic'],
})
).toHaveLength(0)
})
})
// describe('validateRules', () => {
// it('should ', () => {
// expect(
// validateRules({
// rules: JSON.parse(realRules),
// records: [JSON.parse(workbook)],
// })
// ).toBe(undefined)
// })
// })
})

describe('workbookValidation tests', () => {
let actualResult: unknown
Expand Down Expand Up @@ -136,7 +95,6 @@ describe('workbookValidation tests', () => {
{
type: 'logic',
content: { version: 'v:20231212' },
upload: { id: 1 },
},
]
actualResult = validateVersion({ records, rules })
Expand Down Expand Up @@ -218,23 +176,18 @@ describe('workbookValidation tests', () => {
})
})
describe('validateRecord', () => {
let typeRules, record, upload
let typeRules, record
afterEach(() => {
typeRules = undefined
record = undefined
upload = undefined
})
describe('required content', () => {
beforeEach(() => {
upload = undefined
const translatedRecords = translateRecords(
[
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
some: 'thing',
},
Expand All @@ -247,7 +200,7 @@ describe('workbookValidation tests', () => {
})
describe('when a required key item is missing', () => {
it('should return ValidationError', () => {
expect(validateRecord({ upload, record, typeRules })).toEqual(
expect(validateRecord({ record, typeRules })).toEqual(
expect.arrayContaining([
new ValidationError('Value is required for Project_Name__c'),
])
Expand All @@ -256,7 +209,7 @@ describe('workbookValidation tests', () => {
})
describe('when an optional key item is missing', () => {
it('should not have error for missing item', () => {
expect(validateRecord({ upload, record, typeRules })).not.toEqual(
expect(validateRecord({ record, typeRules })).not.toEqual(
expect.arrayContaining([
new ValidationError(
'Value is required for Additional_Address__c'
Expand All @@ -269,15 +222,11 @@ describe('workbookValidation tests', () => {
describe('listVals content', () => {
describe('when a listValue does not match', () => {
beforeEach(() => {
upload = undefined
const translatedRecords = translateRecords(
[
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
Matching_Funds__c: 'Hello World',
},
Expand All @@ -289,7 +238,7 @@ describe('workbookValidation tests', () => {
typeRules = rules['ec1']
})
it('should return a "one of" ValidationError', () => {
const errors = validateRecord({ upload, record, typeRules })
const errors = validateRecord({ record, typeRules })
expect(errors).toEqual(
expect.arrayContaining([
new ValidationError(
Expand All @@ -308,9 +257,6 @@ describe('workbookValidation tests', () => {
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
Total_from_all_funding_sources__c: 'hello world',
},
Expand All @@ -320,7 +266,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['ec1']
expect(validateRecord({ upload, record, typeRules })).toEqual(
expect(validateRecord({ record, typeRules })).toEqual(
expect.arrayContaining([
new ValidationError(
'Data entered in cell is "hello world", but it must be a number with at most 2 decimals'
Expand All @@ -336,9 +282,6 @@ describe('workbookValidation tests', () => {
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
Total_from_all_funding_sources__c: 1234.98,
},
Expand All @@ -348,7 +291,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['ec1']
expect(validateRecord({ upload, record, typeRules })).not.toEqual(
expect(validateRecord({ record, typeRules })).not.toEqual(
expect.arrayContaining([
new ValidationError(
'Data entered in cell is "hello world", but it must be a number with at most 2 decimals'
Expand All @@ -366,9 +309,6 @@ describe('workbookValidation tests', () => {
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
Projected_Con_Start_Date__c: 'hello world',
},
Expand All @@ -378,7 +318,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['ec1']
expect(validateRecord({ upload, record, typeRules })).toEqual(
expect(validateRecord({ record, typeRules })).toEqual(
expect.arrayContaining([
new ValidationError(
'Data entered in cell is "hello world", which is not a valid date.'
Expand All @@ -394,9 +334,6 @@ describe('workbookValidation tests', () => {
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
Projected_Con_Start_Date__c: 1478148420000,
},
Expand All @@ -406,7 +343,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['ec1']
expect(validateRecord({ upload, record, typeRules })).not.toEqual(
expect(validateRecord({ record, typeRules })).not.toEqual(
expect.arrayContaining([
new ValidationError(
'Data entered in cell is "1478148420000", which is not a valid date.'
Expand All @@ -424,9 +361,6 @@ describe('workbookValidation tests', () => {
{
type: 'subrecipient',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
POC_Email_Address__c: 'Hello.World',
},
Expand All @@ -436,7 +370,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['subrecipient']
const errors = validateRecord({ upload, record, typeRules })
const errors = validateRecord({ record, typeRules })
expect(errors).toEqual(
expect.arrayContaining([
new ValidationError(
Expand All @@ -453,9 +387,6 @@ describe('workbookValidation tests', () => {
{
type: 'subrecipient',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
POC_Email_Address__c: '[email protected]',
},
Expand All @@ -465,7 +396,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['subrecipient']
expect(validateRecord({ upload, record, typeRules })).not.toEqual(
expect(validateRecord({ record, typeRules })).not.toEqual(
expect.arrayContaining([
new ValidationError(
'Value entered in cell is "[email protected]". Email must be of the form "[email protected]"'
Expand All @@ -483,9 +414,6 @@ describe('workbookValidation tests', () => {
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
Total_Miles_Planned__c: 'Hello.World',
},
Expand All @@ -495,7 +423,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['ec1']
const errors = validateRecord({ upload, record, typeRules })
const errors = validateRecord({ record, typeRules })
expect(errors).toEqual(
expect.arrayContaining([
new ValidationError(
Expand All @@ -512,9 +440,6 @@ describe('workbookValidation tests', () => {
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
Total_Miles_Planned__c: 42,
},
Expand All @@ -524,7 +449,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['ec1']
expect(validateRecord({ upload, record, typeRules })).not.toEqual(
expect(validateRecord({ record, typeRules })).not.toEqual(
expect.arrayContaining([
new ValidationError("Expected a number, but the value was '42'"),
])
Expand All @@ -540,9 +465,6 @@ describe('workbookValidation tests', () => {
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
Zip_Code_Planned__c: 'asdf_asdf_asdf',
},
Expand All @@ -552,7 +474,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['ec1']
const errors = validateRecord({ upload, record, typeRules })
const errors = validateRecord({ record, typeRules })
expect(errors).toEqual(
expect.arrayContaining([
new ValidationError(
Expand All @@ -569,9 +491,6 @@ describe('workbookValidation tests', () => {
{
type: 'ec1',
subcategory: '1A-Broadband Infrastructure',
upload: {
id: 1,
},
content: {
Zip_Code_Planned__c: 'asdf',
},
Expand All @@ -581,7 +500,7 @@ describe('workbookValidation tests', () => {
)
record = translatedRecords[0]
typeRules = rules['ec1']
expect(validateRecord({ upload, record, typeRules })).not.toEqual(
expect(validateRecord({ record, typeRules })).not.toEqual(
expect.arrayContaining([
new ValidationError(
'Value for Zip_Code_Planned__c cannot be longer than 5 (currently, 4)'
Expand Down
14 changes: 6 additions & 8 deletions api/src/lib/workbookValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const CURRENCY_REGEX_PATTERN = /^\d+(?: \.\d{ 1, 2 })?$/g
const EMAIL_REGEX_PATTERN =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

const BETA_VALIDATION_MESSAGE =
'[BETA] This is a new validation that is running in beta mode (as a warning instead of a blocking error). If you see anything incorrect about this validation, please report it at [email protected]'
// const BETA_VALIDATION_MESSAGE =
// '[BETA] This is a new validation that is running in beta mode (as a warning instead of a blocking error). If you see anything incorrect about this validation, please report it at [email protected]'

const SHOULD_NOT_CONTAIN_PERIOD_REGEX_PATTERN = /^[^.]*$/

Expand Down Expand Up @@ -40,14 +40,15 @@ export type Rule = {
version?: string
}

type FormatterFunction = (value: string | number) => string | number

export interface TranslatedRule extends Rule {
isRequiredFn?: (value: Record<string, string | number>) => boolean
validationFormatters: any[]
validationFormatters: FormatterFunction[]
}

export type WorkbookRecord = {
type: string
upload: { id: number }
content: Record<string, string | number>
subcategory?: string
}
Expand Down Expand Up @@ -156,7 +157,7 @@ export function validateVersion({
return undefined
}

function validateFieldPattern(fieldName, value) {
function validateFieldPattern(fieldName: string, value) {
let error = null
const matchedFieldPatternInfo = FIELD_NAME_TO_PATTERN[fieldName]
if (matchedFieldPatternInfo) {
Expand All @@ -170,11 +171,9 @@ function validateFieldPattern(fieldName, value) {
}

export function validateRecord({
upload,
record,
typeRules,
}: {
upload: unknown
record: Record<string, string | number>
typeRules: Record<string, TranslatedRule>
}) {
Expand Down Expand Up @@ -318,7 +317,6 @@ export function validateRecord({
}
}

// make sure max length is not too long
if (rule.maxLength) {
if (
(rule.dataType === 'String' || rule.dataType === 'String-Fixed') &&
Expand Down

0 comments on commit 7dc603d

Please sign in to comment.