diff --git a/packages/builder/src/helpers/validation/validation.js b/packages/builder/src/helpers/validation/validation.js new file mode 100644 index 0000000000..7f44f33663 --- /dev/null +++ b/packages/builder/src/helpers/validation/validation.js @@ -0,0 +1,15 @@ +import { writable, derived } from 'svelte/store' + +export function createValidationStore(initialValue, ...validators) { + + const value = writable(initialValue || '') + const error = derived(value, $v => validate($v, validators)) + + return [value, error] +} + +function validate(value, validators) { + const failing = validators.find(v => v(value) !== true) + + return failing && failing(value) +} \ No newline at end of file diff --git a/packages/builder/src/helpers/validation/validators.js b/packages/builder/src/helpers/validation/validators.js new file mode 100644 index 0000000000..d4ae90381e --- /dev/null +++ b/packages/builder/src/helpers/validation/validators.js @@ -0,0 +1,12 @@ +function emailValidator (value) { + return (value && !!value.match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)) || 'Please enter a valid email' + } + +function requiredValidator (value) { + return (value !== undefined && value !== null && value !== '') || 'This field is required' + } + +export { + emailValidator, + requiredValidator +} \ No newline at end of file