Skip to content

Commit

Permalink
make formErrors ?
Browse files Browse the repository at this point in the history
  • Loading branch information
lukad committed Feb 3, 2025
1 parent 7be7788 commit 3a5bdbc
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions next/src/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface ValidationError {
}

export interface ValidationResult {
formErrors: Record<string, string> | undefined
formErrors?: Record<string, string>
}

/**
Expand All @@ -44,11 +44,15 @@ export interface ValidationResult {
* @returns The validation result
*/
function validate(value: SchemaValue, schema: JsfSchema): ValidationResult {
const result: ValidationResult = {}
const errors = validateSchema(value, schema)
const formErrors = validationErrorsToFormErrors(errors)

return {
formErrors: validationErrorsToFormErrors(errors),
if (formErrors) {
result.formErrors = formErrors
}

return result
}

/**
Expand All @@ -64,9 +68,9 @@ function validate(value: SchemaValue, schema: JsfSchema): ValidationResult {
* ])
* // { '.address.street': 'must be a string' }
*/
function validationErrorsToFormErrors(errors: ValidationError[]): Record<string, string> | undefined {
function validationErrorsToFormErrors(errors: ValidationError[]): Record<string, string> | null {
if (errors.length === 0) {
return undefined
return null
}

return errors.reduce((acc: Record<string, string>, error) => {
Expand Down

0 comments on commit 3a5bdbc

Please sign in to comment.