Skip to content

Commit

Permalink
fix: unique values
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 17, 2025
1 parent 4841b48 commit b65993d
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/components/form/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type TextFieldProps = Omit<
validateOptions?: ValidateOptions
dirty?: boolean
split?: string | RegExp
unique?: boolean
uniqueCaseInsensitive?: boolean
}

// https://formik.org/docs/examples/with-material-ui
Expand All @@ -40,6 +42,8 @@ const TextField: FC<TextFieldProps> = ({
type = "text",
required = false,
dirty = false,
unique = false,
uniqueCaseInsensitive = false,
split,
validateOptions,
...otherTextFieldProps
Expand All @@ -49,7 +53,27 @@ const TextField: FC<TextFieldProps> = ({
const dotPath = name.split(".")

let _schema: Schema = schema
if (split) _schema = YupArray().of(_schema)
if (split) {
_schema = YupArray().of(_schema)
if (unique || uniqueCaseInsensitive) {
_schema = _schema.test({
message: "cannot have duplicates",
test: values => {
if (Array.isArray(values) && values.length >= 2) {
return (
new Set(
uniqueCaseInsensitive && typeof values[0] === "string"
? values.map(value => value.toLowerCase())
: values,
).size === values.length
)
}

return true
},
})
}
}
if (required) {
_schema = _schema.required()
if (split) _schema = (_schema as ArraySchema<string[], any>).min(1)
Expand Down

0 comments on commit b65993d

Please sign in to comment.