-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds ValidationError type into validator (#106)
* Adds ValidationError type into validator * Adds field name to default error messages
- Loading branch information
Showing
17 changed files
with
101 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,93 @@ | ||
import type { FieldValidationErrors, ValidationError } from "../elementSpec"; | ||
import type { FieldNameToValueMap } from "../fieldViews/helpers"; | ||
import type { FieldDescriptions } from "../types/Element"; | ||
|
||
type Validator = (fieldValue: unknown) => string[]; | ||
type Validator = (fieldValue: unknown, fieldName: string) => ValidationError[]; | ||
|
||
export const createValidator = ( | ||
fieldValidationMap: Record<string, Validator[]> | ||
) => <FDesc extends FieldDescriptions<string>>( | ||
fieldValues: FieldNameToValueMap<FDesc> | ||
): Record<string, string[]> => { | ||
const errors: Record<string, string[]> = {}; | ||
): FieldValidationErrors => { | ||
const errors: FieldValidationErrors = {}; | ||
|
||
for (const fieldName in fieldValidationMap) { | ||
const validators = fieldValidationMap[fieldName]; | ||
const value = fieldValues[fieldName]; | ||
const fieldErrors = validators.flatMap((validator) => validator(value)); | ||
const fieldErrors = validators.flatMap((validator) => | ||
validator(value, fieldName) | ||
); | ||
errors[fieldName] = fieldErrors; | ||
} | ||
return errors; | ||
}; | ||
|
||
export const htmlMaxLength = (maxLength: number): Validator => (value) => { | ||
export const htmlMaxLength = ( | ||
maxLength: number, | ||
customMessage: string | undefined = undefined | ||
): Validator => (value, field) => { | ||
if (typeof value !== "string") { | ||
throw new Error(`[htmlMaxLength]: value is not of type string`); | ||
} | ||
const el = document.createElement("div"); | ||
el.innerHTML = value; | ||
if (el.innerText.length > maxLength) { | ||
return [`Too long: ${el.innerText.length}/${maxLength}`]; | ||
return [ | ||
{ | ||
error: `Too long: ${value.length}/${maxLength}`, | ||
message: | ||
customMessage ?? `${field} is too long: ${value.length}/${maxLength}`, | ||
}, | ||
]; | ||
} | ||
return []; | ||
}; | ||
|
||
export const maxLength = (maxLength: number): Validator => (value) => { | ||
export const maxLength = ( | ||
maxLength: number, | ||
customMessage: string | undefined = undefined | ||
): Validator => (value, field) => { | ||
if (typeof value !== "string") { | ||
throw new Error(`[maxLength]: value is not of type string`); | ||
} | ||
if (value.length > maxLength) { | ||
return [`Too long: ${value.length}/${maxLength}`]; | ||
return [ | ||
{ | ||
error: `Too long: ${value.length}/${maxLength}`, | ||
message: | ||
customMessage ?? `${field} is too long: ${value.length}/${maxLength}`, | ||
}, | ||
]; | ||
} | ||
return []; | ||
}; | ||
|
||
export const htmlRequired = (): Validator => (value) => { | ||
export const htmlRequired = ( | ||
customMessage: string | undefined = undefined | ||
): Validator => (value, field) => { | ||
if (typeof value !== "string") { | ||
throw new Error(`[maxLength]: value is not of type string`); | ||
} | ||
const el = document.createElement("div"); | ||
el.innerHTML = value; | ||
if (!el.innerText.length) { | ||
return ["Required"]; | ||
return [ | ||
{ error: "Required", message: customMessage ?? `${field} is required` }, | ||
]; | ||
} | ||
return []; | ||
}; | ||
|
||
export const required = (): Validator => (value) => { | ||
export const required = ( | ||
customMessage: string | undefined = undefined | ||
): Validator => (value, field) => { | ||
if (typeof value !== "string") { | ||
throw new Error(`[maxLength]: value is not of type string`); | ||
} | ||
if (!value.length) { | ||
return ["Required"]; | ||
return [ | ||
{ error: "Required", message: customMessage ?? `${field} is required` }, | ||
]; | ||
} | ||
return []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import type { FieldValidationErrors } from "../elementSpec"; | ||
import type { FieldNameToValueMap } from "../fieldViews/helpers"; | ||
import type { FieldDescriptions, FieldNameToField } from "./Element"; | ||
import type { Errors } from "./Errors"; | ||
|
||
export type Consumer< | ||
ConsumerResult, | ||
FDesc extends FieldDescriptions<string> | ||
> = ( | ||
fieldValues: FieldNameToValueMap<FDesc>, | ||
errors: Errors, | ||
errors: FieldValidationErrors, | ||
updateFields: (fieldValues: FieldNameToValueMap<FDesc>) => void, | ||
fields: FieldNameToField<FDesc> | ||
) => ConsumerResult; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.