Skip to content

Commit

Permalink
fix: check for existing field before validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanvier committed Oct 15, 2024
1 parent b9d0b61 commit b609dab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wgr-sa/nuxt-form",
"description": "Form builder for Nuxt",
"version": "0.9.0",
"version": "0.9.1",
"repository": "https://github.com/WGR-SA/nuxt-form.git",
"author": "jeanvier",
"license": "MIT",
Expand Down
32 changes: 17 additions & 15 deletions src/runtime/composables/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,28 @@ export const useFormValidator = () => {
if (!form.validator.rules[field]) return []

const errors: string[] = []
const fieldValue = form.data.state[field]
if (field in form.data.state) {
const fieldValue = form.data.state[field]

for (const rule of form.validator.rules[field]) {
const validatorName = rule.$params.type as keyof typeof validators
const validator = validators[validatorName]
for (const rule of form.validator.rules[field]) {
const validatorName = rule.$params.type as keyof typeof validators
const validator = validators[validatorName]

if (!validator) {
console.error(`Validator ${validatorName} not found`)
continue
}
if (!validator) {
console.error(`Validator ${validatorName} not found`)
continue
}

if (['isEmail', 'isNumber'].includes(validatorName) && !fieldValue) continue
if (['isEmail', 'isNumber'].includes(validatorName) && !fieldValue) continue

if (fieldValue !== undefined) {
const expectedType = getExpectedType(validatorName)
const convertedValue = convertValue(fieldValue, expectedType)
if (fieldValue !== undefined) {
const expectedType = getExpectedType(validatorName)
const convertedValue = convertValue(fieldValue, expectedType)

if (!validator(convertedValue, ...(rule.$params.options || [])) &&
(fieldValue.length > 0 && fieldValue !== 'false' || ['error', 'validate'].includes(form.state.status))) {
errors.push(rule.custom_message ?? rule.$message)
if (!validator(convertedValue, ...(rule.$params.options || [])) &&
(fieldValue.length > 0 && fieldValue !== 'false' || ['error', 'validate'].includes(form.state.status))) {
errors.push(rule.custom_message ?? rule.$message)
}
}
}
}
Expand Down

0 comments on commit b609dab

Please sign in to comment.