Handle unexpected errors during validation
This commit is contained in:
parent
9db180b166
commit
f66a8b888f
|
@ -1,6 +1,7 @@
|
||||||
import { capitalise } from "helpers"
|
import { capitalise } from "helpers"
|
||||||
import { object } from "yup"
|
import { object } from "yup"
|
||||||
import { writable, get } from "svelte/store"
|
import { writable, get } from "svelte/store"
|
||||||
|
import { notifications } from "@budibase/bbui"
|
||||||
|
|
||||||
export const createValidationStore = () => {
|
export const createValidationStore = () => {
|
||||||
const DEFAULT = {
|
const DEFAULT = {
|
||||||
|
@ -24,9 +25,15 @@ export const createValidationStore = () => {
|
||||||
// clear the previous errors
|
// clear the previous errors
|
||||||
const properties = Object.keys(validator)
|
const properties = Object.keys(validator)
|
||||||
properties.forEach(property => (get(validation).errors[property] = null))
|
properties.forEach(property => (get(validation).errors[property] = null))
|
||||||
|
|
||||||
|
let validationError = false
|
||||||
try {
|
try {
|
||||||
await obj.validate(values, { abortEarly: false })
|
await obj.validate(values, { abortEarly: false })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (!error.inner) {
|
||||||
|
notifications.error("Unexpected validation error", error)
|
||||||
|
validationError = true
|
||||||
|
} else {
|
||||||
error.inner.forEach(err => {
|
error.inner.forEach(err => {
|
||||||
validation.update(store => {
|
validation.update(store => {
|
||||||
store.errors[err.path] = capitalise(err.message)
|
store.errors[err.path] = capitalise(err.message)
|
||||||
|
@ -34,9 +41,10 @@ export const createValidationStore = () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let valid
|
let valid
|
||||||
if (properties.length) {
|
if (properties.length && !validationError) {
|
||||||
valid = await obj.isValid(values)
|
valid = await obj.isValid(values)
|
||||||
} else {
|
} else {
|
||||||
// don't say valid until validators have been loaded
|
// don't say valid until validators have been loaded
|
||||||
|
|
Loading…
Reference in New Issue