fix: text field validation for minLength: 1, required: false (#13124)

Fixes #13113

### How?

Does not rely on JS falseyness, instead explicitly checking for null &
undefined


I'm not actually certain this is the approach we want to take. Some
people might interpret "required" as not null, not-undefined and min
length > 1 in the case of strings. If they do, this change to the
behavior in the not-required case will break their expectations
This commit is contained in:
Chandler Gonzales
2025-07-21 06:23:44 -07:00
committed by GitHub
parent dce898d7ca
commit af2ddff203
2 changed files with 6 additions and 1 deletions

View File

@@ -61,6 +61,11 @@ describe('Field Validations', () => {
const result = text(val, { ...options, minLength: 10 })
expect(result).toBe(true)
})
it('should validate minLength with empty string', () => {
const val = ''
const result = text(val, { ...options, required: false, minLength: 1 })
expect(result).toBe('validation:longerThanMin')
})
it('should validate an array of texts', async () => {
const val = ['test']
const result = text(val, { ...options, hasMany: true })

View File

@@ -61,7 +61,7 @@ export const text: TextFieldValidation = (
let maxLength!: number
if (!required) {
if (!value) {
if (value === undefined || value === null) {
return true
}
}