Skip to content

Commit

Permalink
update forms to use new express validator classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rgforsyth committed Jun 5, 2023
1 parent b456c03 commit fdc5263
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,7 @@ export default class AmendAccessibilityNeedsForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}

private checkForNoChangesError(validationResult: Result<ValidationError>): boolean | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ export default class AmendAdditionalInformationForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ export default class AmendComplexityLevelForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ export default class AmendDesiredOutcomesForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}

private checkForNoChangesError(validationResult: Result<ValidationError>): boolean | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ export default class AmendEmploymentResponsibilitiesForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}

private checkForNoChangesError(validationResult: Result<ValidationError>): boolean | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,7 @@ export default class AmendNeedsAndRequirementsIntepreterForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}

private checkForNoChangesError(validationResult: Result<ValidationError>): boolean | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ export default class AttendanceFeedbackForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ export default class BehaviourFeedbackForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}

private get notifyProbationPractitioner(): boolean {
Expand Down
8 changes: 1 addition & 7 deletions server/routes/caseNotes/add/AddNewCaseNoteForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ export default class AddNewCaseNoteForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ export default class CurrentLocationForm {
return null
}

return {
errors: this.result.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(this.result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ export default class EnforceableDaysForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,7 @@ export default class ExpectedReleaseDateForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}

private get expectedReleaseDate(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ export default class NeedsAndRequirementsForm {
return null
}

return {
errors: this.result.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(this.result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ export default class ConfirmOasysRiskInformationForm {
return null
}

return {
errors: this.result.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(this.result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ export default class EditOasysRiskInformationForm {
return null
}

return {
errors: this.result.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(this.result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ export default class UpdateServiceCategoriesForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ export default class ComplexityLevelForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ export default class DesiredOutcomesForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ export default class ReferralCancellationReasonForm {
return null
}

return {
errors: validationResult.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(validationResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { UpdateDraftActionPlanParams } from '../../../../services/interventionsS
import errorMessages from '../../../../utils/errorMessages'
import FormUtils from '../../../../utils/formUtils'
import { FormValidationError } from '../../../../utils/formValidationError'
import {forEach} from "iterall";

export default class ActionPlanNumberOfSessionsForm {
private constructor(private readonly request: Request, private readonly result: Result<ValidationError>) {}
Expand Down Expand Up @@ -42,13 +43,7 @@ export default class ActionPlanNumberOfSessionsForm {
return null
}

return {
errors: this.result.array().map(validationError => ({
formFields: [validationError.param],
errorSummaryLinkedField: validationError.param,
message: validationError.msg,
})),
}
return FormUtils.getFormValidationError(this.result)
}

get paramsForUpdate(): Partial<UpdateDraftActionPlanParams> {
Expand Down
49 changes: 49 additions & 0 deletions server/utils/formUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,53 @@ export default class FormUtils {
})),
}
}

static getFormValidationError( result: Result<ValidationError>): FormValidationError{
let summaryError:FormValidationError = {errors: []}
let errors = result.array()
errors.forEach(function (value) {
summaryError.errors.push(FormUtils.errorSummary(value))
})
return summaryError
}

static errorSummary(validationError: ValidationError) {
switch (validationError.type){
case 'field':
return{
formFields: [validationError.path],
errorSummaryLinkedField: validationError.path,
message: validationError.msg
}
// case 'alternative':
// return validationError.nestedErrors.map(error => ({
// formFields: [error.path],
// errorSummaryLinkedField: error.path,
// message: error.msg
// })
// )
// return null
// case 'alternative_grouped':
// // validationError.nestedErrors.map(error => ({
// // formFields: [error.],
// // errorSummaryLinkedField: error.path,
// // message: error.msg
// // })
// // return{
// // formFields: [validationError],
// // errorSummaryLinkedField: validationError.path,
// // message: validationError.msg
// // }
// // this is an AlternativeValidationError or GroupedAlternativeValidationError
// return null
// case 'unknown_fields':
// // this is an UnknownFieldValidationError
// const fields = validationError.fields.map(field => field.path).join(', ');
// return null

default:
// Not a known type.
throw new Error(`Not a known express-validator error: ${validationError}`);
}
}
}

0 comments on commit fdc5263

Please sign in to comment.