@michalzard/svelte-forms 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +82 -8
- package/bun.lock +1 -0
- package/dist/index.js +18 -8
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,15 +1,89 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Svelte Form Validation
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Svelte 5 Form validation made simple.
|
|
4
|
+
In order to define `intialValues` you pass [valibot](https://www.npmjs.com/package/valibot) schema.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
## Usage example:
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
const { values, errors, touched, markTouched, handleSubmit } = useForm({
|
|
10
|
+
initialValues: {
|
|
11
|
+
email: "",
|
|
12
|
+
password: "",
|
|
13
|
+
},
|
|
14
|
+
schema: signInSchema,
|
|
15
|
+
onSubmit(values) {
|
|
16
|
+
console.log("submitted ", values);
|
|
17
|
+
// TODO: add api call
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Valibot schema that you'd use for values(email,password) i.e:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
export const passwordPolicy = v.pipe(
|
|
26
|
+
v.string(),
|
|
27
|
+
v.trim(),
|
|
28
|
+
v.minLength(8, "Password must be at least 8 characters long."),
|
|
29
|
+
v.maxLength(64, "Password must be at most 64 characters long."),
|
|
30
|
+
v.regex(/[A-Z]/, "Password must contain at least 1 uppercase letter."),
|
|
31
|
+
v.regex(/[0-9]/, "Password must contain at least 1 number."),
|
|
32
|
+
v.regex(
|
|
33
|
+
/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/,
|
|
34
|
+
"Password must contain at least 1 special character.",
|
|
35
|
+
),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
export const signInSchema = v.object({
|
|
39
|
+
email: v.pipe(
|
|
40
|
+
v.string(),
|
|
41
|
+
v.trim(),
|
|
42
|
+
v.email("Please provide a valid email address."),
|
|
43
|
+
),
|
|
44
|
+
password: passwordPolicy,
|
|
45
|
+
});
|
|
7
46
|
```
|
|
8
47
|
|
|
9
|
-
|
|
48
|
+
In order to capture submitted values you simply do:
|
|
10
49
|
|
|
11
|
-
```
|
|
12
|
-
|
|
50
|
+
```ts
|
|
51
|
+
<form onsubmit={handleSubmit}>
|
|
13
52
|
```
|
|
14
53
|
|
|
15
|
-
|
|
54
|
+
To update each value you can simply use `bind:value` to individual writable signals,i.e `$values.email`
|
|
55
|
+
To mark any input element as `touched` you can use `markTouched` function with the name of the field in order for form to track its `touched` state,that way you can display errors conditionally only if you interacted with the form input, without using `$touched.<fieldname>`, you'd be displaying errors immediately.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
<Input
|
|
59
|
+
id="email"
|
|
60
|
+
type="email"
|
|
61
|
+
placeholder="johndoe@example.com"
|
|
62
|
+
bind:value={$values.email}
|
|
63
|
+
onblur={() => markTouched("email")}
|
|
64
|
+
/>
|
|
65
|
+
{#if $touched.email && $errors.email}
|
|
66
|
+
<FieldError>{$errors.email}</FieldError>
|
|
67
|
+
{/if}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
If you'd like to adjust how fast validation parsing gets executed since by default we have **debounce interval** of `50ms` you can adjust it by doing:
|
|
71
|
+
|
|
72
|
+
Example on how to define `500ms` debounce(useful for searchbars that trigger api calls):
|
|
73
|
+
<br/>
|
|
74
|
+
`debounceInterval: 500`
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const { values, errors, touched, markTouched, handleSubmit } = useForm({
|
|
78
|
+
initialValues: {
|
|
79
|
+
email: "",
|
|
80
|
+
password: "",
|
|
81
|
+
},
|
|
82
|
+
schema: signInSchema,
|
|
83
|
+
debounceInterval:500
|
|
84
|
+
onSubmit(values) {
|
|
85
|
+
console.log("submitted ", values);
|
|
86
|
+
// TODO: add api call
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
```
|
package/bun.lock
CHANGED
package/dist/index.js
CHANGED
|
@@ -2,27 +2,37 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
6
8
|
var __toCommonJS = (from) => {
|
|
7
|
-
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
8
10
|
if (entry)
|
|
9
11
|
return entry;
|
|
10
12
|
entry = __defProp({}, "__esModule", { value: true });
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
16
21
|
__moduleCache.set(from, entry);
|
|
17
22
|
return entry;
|
|
18
23
|
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
19
29
|
var __export = (target, all) => {
|
|
20
30
|
for (var name in all)
|
|
21
31
|
__defProp(target, name, {
|
|
22
32
|
get: all[name],
|
|
23
33
|
enumerable: true,
|
|
24
34
|
configurable: true,
|
|
25
|
-
set: (
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
26
36
|
});
|
|
27
37
|
};
|
|
28
38
|
|