solid-tiny-form 0.1.0 → 0.2.0

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 solid tiny
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 solid tiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,96 +1,96 @@
1
- # solid-tiny-form
2
-
3
- A tiny SolidJS form state manager inspired by `react-hook-form`, but designed around Solid's fine-grained reactivity and `createStore`.
4
-
5
- ## Features
6
-
7
- - `createForm` API for registering fields, setting values, submitting, resetting, and validating.
8
- - Form state is stored in a Solid store from `solid-js/store`.
9
- - Pluggable resolver interface for schema validation libraries.
10
- - Built-in ArkType resolver via `arktypeResolver`.
11
- - Lightweight field-level rules for common cases.
12
-
13
- ## Installation
14
-
15
- ```sh
16
- pnpm add solid-tiny-form solid-js
17
- ```
18
-
19
- Install ArkType when using the optional ArkType resolver:
20
-
21
- ```sh
22
- pnpm add arktype
23
- ```
24
-
25
- The ArkType adapter supports `arktype@^2.1.27` and is exposed from `solid-tiny-form/resolvers/arktype`.
26
-
27
- ## Usage
28
-
29
- ```tsx
30
- import { type } from "arktype";
31
- import { createForm } from "solid-tiny-form";
32
- import { arktypeResolver } from "solid-tiny-form/resolvers/arktype";
33
-
34
- const schema = type({
35
- email: "string.email",
36
- age: "number >= 18",
37
- });
38
-
39
- function Example() {
40
- const form = createForm({
41
- defaultValues: {
42
- email: "",
43
- age: 18,
44
- },
45
- resolver: arktypeResolver(schema),
46
- mode: "onBlur",
47
- });
48
-
49
- const onSubmit = form.handleSubmit((values) => {
50
- console.log(values);
51
- });
52
-
53
- return (
54
- <form onSubmit={onSubmit}>
55
- <input {...form.register("email")} />
56
- <input type="number" {...form.register("age", { valueAsNumber: true })} />
57
- <button disabled={form.formState.isSubmitting}>Submit</button>
58
- </form>
59
- );
60
- }
61
- ```
62
-
63
- ## API
64
-
65
- ### `createForm(options)`
66
-
67
- Creates a form controller.
68
-
69
- ```ts
70
- const form = createForm({
71
- defaultValues,
72
- resolver,
73
- context,
74
- mode: "onSubmit",
75
- });
76
- ```
77
-
78
- Validation modes are `onSubmit`, `onBlur`, `onChange`, and `all`.
79
-
80
- ### Resolver interface
81
-
82
- Resolvers receive current values and return parsed values plus field errors.
83
-
84
- ```ts
85
- type Resolver<TValues, TContext> = (
86
- values: TValues,
87
- context: {
88
- name?: string;
89
- context?: TContext;
90
- }
91
- ) =>
92
- | { values: TValues; errors: Record<string, FieldError> }
93
- | Promise<{ values: TValues; errors: Record<string, FieldError> }>;
94
- ```
95
-
96
- This keeps the core form independent from any validation library.
1
+ # solid-tiny-form
2
+
3
+ A tiny SolidJS form state manager inspired by `react-hook-form`, but designed around Solid's fine-grained reactivity and `createStore`.
4
+
5
+ ## Features
6
+
7
+ - `createForm` API for registering fields, setting values, submitting, resetting, and validating.
8
+ - Form state is stored in a Solid store from `solid-js/store`.
9
+ - Pluggable resolver interface for schema validation libraries.
10
+ - Built-in ArkType resolver via `arktypeResolver`.
11
+ - Lightweight field-level rules for common cases.
12
+
13
+ ## Installation
14
+
15
+ ```sh
16
+ pnpm add solid-tiny-form solid-js
17
+ ```
18
+
19
+ Install ArkType when using the optional ArkType resolver:
20
+
21
+ ```sh
22
+ pnpm add arktype
23
+ ```
24
+
25
+ The ArkType adapter supports `arktype@^2.1.27` and is exposed from `solid-tiny-form/resolvers/arktype`.
26
+
27
+ ## Usage
28
+
29
+ ```tsx
30
+ import { type } from "arktype";
31
+ import { createForm } from "solid-tiny-form";
32
+ import { arktypeResolver } from "solid-tiny-form/resolvers/arktype";
33
+
34
+ const schema = type({
35
+ email: "string.email",
36
+ age: "number >= 18",
37
+ });
38
+
39
+ function Example() {
40
+ const form = createForm({
41
+ defaultValues: {
42
+ email: "",
43
+ age: 18,
44
+ },
45
+ resolver: arktypeResolver(schema),
46
+ mode: "onBlur",
47
+ });
48
+
49
+ const onSubmit = form.handleSubmit((values) => {
50
+ console.log(values);
51
+ });
52
+
53
+ return (
54
+ <form onSubmit={onSubmit}>
55
+ <input {...form.register("email")} />
56
+ <input type="number" {...form.register("age", { valueAsNumber: true })} />
57
+ <button disabled={form.formState.isSubmitting}>Submit</button>
58
+ </form>
59
+ );
60
+ }
61
+ ```
62
+
63
+ ## API
64
+
65
+ ### `createForm(options)`
66
+
67
+ Creates a form controller.
68
+
69
+ ```ts
70
+ const form = createForm({
71
+ defaultValues,
72
+ resolver,
73
+ context,
74
+ mode: "onSubmit",
75
+ });
76
+ ```
77
+
78
+ Validation modes are `onSubmit`, `onBlur`, `onChange`, and `all`.
79
+
80
+ ### Resolver interface
81
+
82
+ Resolvers receive current values and return parsed values plus field errors.
83
+
84
+ ```ts
85
+ type Resolver<TValues, TContext> = (
86
+ values: TValues,
87
+ context: {
88
+ name?: string;
89
+ context?: TContext;
90
+ }
91
+ ) =>
92
+ | { values: TValues; errors: Record<string, FieldError> }
93
+ | Promise<{ values: TValues; errors: Record<string, FieldError> }>;
94
+ ```
95
+
96
+ This keeps the core form independent from any validation library.
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as FieldPath, c as FieldValues, d as RegisterOptions, f as Resolver, g as ValidationMode, h as SetValueOptions, i as FieldFlags, l as Form, m as ResolverResult, n as FieldError, o as FieldPathValue, p as ResolverContext, r as FieldErrors, s as FieldRegistration, t as CreateFormOptions, u as FormState } from "./types-CzHtMISe.mjs";
1
+ import { a as FieldPath, c as FieldValues, d as RegisterOptions, f as Resolver, g as ValidationMode, h as SetValueOptions, i as FieldFlags, l as Form, m as ResolverResult, n as FieldError, o as FieldPathValue, p as ResolverContext, r as FieldErrors, s as FieldRegistration, t as CreateFormOptions, u as FormState } from "./types-DL1ynFcc.mjs";
2
2
 
3
3
  //#region src/create-form.d.ts
4
4
  /** Create a Solid-first form controller backed by createStore. */
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { a as isEqual, i as isEmptyObject, n as getEventValue, o as setPath, r as getPath, t as cloneValue } from "./utils-jVw2-Xts.mjs";
1
2
  import { createStore, produce, reconcile, unwrap } from "solid-js/store";
2
3
  //#region src/create-form/async.ts
3
4
  /** Run a promise from a synchronous event handler and surface failures globally. */
@@ -9,65 +10,6 @@ function runAsync(task) {
9
10
  });
10
11
  }
11
12
  //#endregion
12
- //#region src/utils.ts
13
- /** Return a dot path split into store path segments. */
14
- function toPath(name) {
15
- return name.split(".").filter(Boolean);
16
- }
17
- /** Read a nested value from an object by dot path. */
18
- function getPath(source, name) {
19
- let value = source;
20
- for (const key of toPath(name)) {
21
- if (value == null || typeof value !== "object") return;
22
- value = value[key];
23
- }
24
- return value;
25
- }
26
- /** Set a nested value on a cloned object by dot path. */
27
- function setPath(source, name, value) {
28
- const root = cloneValue(source);
29
- const path = toPath(name);
30
- let cursor = root;
31
- for (const [index, key] of path.entries()) {
32
- if (index === path.length - 1) {
33
- cursor[key] = value;
34
- break;
35
- }
36
- const next = cursor[key];
37
- if (next == null || typeof next !== "object" || Array.isArray(next)) cursor[key] = {};
38
- cursor = cursor[key];
39
- }
40
- return root;
41
- }
42
- /** Create a JSON-compatible deep clone. */
43
- function cloneValue(value) {
44
- if (value == null || typeof value !== "object") return value;
45
- return JSON.parse(JSON.stringify(value));
46
- }
47
- /** Compare two JSON-compatible values. */
48
- function isEqual(left, right) {
49
- return JSON.stringify(left) === JSON.stringify(right);
50
- }
51
- /** Return true when an object has no own enumerable keys. */
52
- function isEmptyObject(value) {
53
- return Object.keys(value).length === 0;
54
- }
55
- /** Extract a form value from a DOM event or raw value. */
56
- function getEventValue(valueOrEvent) {
57
- if (!isEventLike(valueOrEvent)) return valueOrEvent;
58
- const target = valueOrEvent.currentTarget ?? valueOrEvent.target;
59
- if (!isValueTarget(target)) return;
60
- if (target.type === "checkbox") return Boolean(target.checked);
61
- if (target.type === "number") return target.value === "" ? void 0 : Number(target.value);
62
- return target.value;
63
- }
64
- function isEventLike(value) {
65
- return value != null && typeof value === "object" && ("target" in value || "currentTarget" in value);
66
- }
67
- function isValueTarget(target) {
68
- return target != null && typeof target === "object" && ("value" in target || "checked" in target);
69
- }
70
- //#endregion
71
13
  //#region src/create-form/state.ts
72
14
  /** Create the initial form state object. */
73
15
  function createInitialFormState(defaultValues) {
@@ -1,10 +1,8 @@
1
- import { c as FieldValues, f as Resolver } from "../types-CzHtMISe.mjs";
1
+ import { c as FieldValues, f as Resolver } from "../types-DL1ynFcc.mjs";
2
2
  import { Type } from "arktype";
3
3
 
4
4
  //#region src/resolvers/arktype.d.ts
5
- /** A supported ArkType schema. */
6
- type ArkTypeSchema<TValues extends FieldValues = FieldValues> = Type<TValues>;
7
5
  /** Create a resolver for ArkType schemas. */
8
- declare function arktypeResolver<TValues extends FieldValues>(schema: ArkTypeSchema<TValues>): Resolver<TValues>;
6
+ declare function arktypeResolver<TValues extends FieldValues>(schema: Type<TValues>): Resolver<TValues>;
9
7
  //#endregion
10
- export { ArkTypeSchema, arktypeResolver };
8
+ export { arktypeResolver };
@@ -1,7 +1,9 @@
1
+ import { r as getPath } from "../utils-jVw2-Xts.mjs";
1
2
  //#region src/resolvers/arktype.ts
2
3
  /** Create a resolver for ArkType schemas. */
3
4
  function arktypeResolver(schema) {
4
- return (values) => {
5
+ return (values, ctx) => {
6
+ if (ctx.name) return validateField(schema, values, ctx.name);
5
7
  const result = schema(values);
6
8
  const errors = readArkTypeErrors(result);
7
9
  return {
@@ -10,6 +12,21 @@ function arktypeResolver(schema) {
10
12
  };
11
13
  };
12
14
  }
15
+ function validateField(schema, values, name) {
16
+ try {
17
+ const errors = readArkTypeErrors(schema.get(...name.split("."))(getPath(values, name)));
18
+ return {
19
+ errors: errors ? prefixFieldErrors(name, errors) : {},
20
+ values
21
+ };
22
+ } catch (err) {
23
+ console.error(err);
24
+ return {
25
+ errors: {},
26
+ values
27
+ };
28
+ }
29
+ }
13
30
  function readArkTypeErrors(result) {
14
31
  if (!isArkErrors(result)) return;
15
32
  const errors = {};
@@ -20,6 +37,14 @@ function readArkTypeErrors(result) {
20
37
  };
21
38
  return errors;
22
39
  }
40
+ function prefixFieldErrors(name, errors) {
41
+ if (errors.root) return { [name]: errors.root };
42
+ return Object.fromEntries(Object.entries(errors).map(([path, error]) => [joinPath(name, path), error]));
43
+ }
44
+ function joinPath(name, path) {
45
+ if (!path || path === "root") return name;
46
+ return `${name}.${path}`;
47
+ }
23
48
  function isArkErrors(value) {
24
49
  return Array.isArray(value) && "byPath" in value && typeof value.summary === "string";
25
50
  }
@@ -109,7 +109,7 @@ interface CreateFormOptions<TValues extends FieldValues = FieldValues, TContext
109
109
  /** Extra context passed to the resolver. */
110
110
  context?: TContext;
111
111
  /** Initial form values. */
112
- defaultValues?: Partial<TValues>;
112
+ defaultValues?: Partial<NoInfer<TValues>>;
113
113
  /** When validation should run automatically. Defaults to onSubmit. */
114
114
  mode?: ValidationMode;
115
115
  /** Schema or custom validation resolver. */
@@ -0,0 +1,60 @@
1
+ //#region src/utils.ts
2
+ /** Return a dot path split into store path segments. */
3
+ function toPath(name) {
4
+ return name.split(".").filter(Boolean);
5
+ }
6
+ /** Read a nested value from an object by dot path. */
7
+ function getPath(source, name) {
8
+ let value = source;
9
+ for (const key of toPath(name)) {
10
+ if (value == null || typeof value !== "object") return;
11
+ value = value[key];
12
+ }
13
+ return value;
14
+ }
15
+ /** Set a nested value on a cloned object by dot path. */
16
+ function setPath(source, name, value) {
17
+ const root = cloneValue(source);
18
+ const path = toPath(name);
19
+ let cursor = root;
20
+ for (const [index, key] of path.entries()) {
21
+ if (index === path.length - 1) {
22
+ cursor[key] = value;
23
+ break;
24
+ }
25
+ const next = cursor[key];
26
+ if (next == null || typeof next !== "object" || Array.isArray(next)) cursor[key] = {};
27
+ cursor = cursor[key];
28
+ }
29
+ return root;
30
+ }
31
+ /** Create a JSON-compatible deep clone. */
32
+ function cloneValue(value) {
33
+ if (value == null || typeof value !== "object") return value;
34
+ return JSON.parse(JSON.stringify(value));
35
+ }
36
+ /** Compare two JSON-compatible values. */
37
+ function isEqual(left, right) {
38
+ return JSON.stringify(left) === JSON.stringify(right);
39
+ }
40
+ /** Return true when an object has no own enumerable keys. */
41
+ function isEmptyObject(value) {
42
+ return Object.keys(value).length === 0;
43
+ }
44
+ /** Extract a form value from a DOM event or raw value. */
45
+ function getEventValue(valueOrEvent) {
46
+ if (!isEventLike(valueOrEvent)) return valueOrEvent;
47
+ const target = valueOrEvent.currentTarget ?? valueOrEvent.target;
48
+ if (!isValueTarget(target)) return;
49
+ if (target.type === "checkbox") return Boolean(target.checked);
50
+ if (target.type === "number") return target.value === "" ? void 0 : Number(target.value);
51
+ return target.value;
52
+ }
53
+ function isEventLike(value) {
54
+ return value != null && typeof value === "object" && ("target" in value || "currentTarget" in value);
55
+ }
56
+ function isValueTarget(target) {
57
+ return target != null && typeof target === "object" && ("value" in target || "checked" in target);
58
+ }
59
+ //#endregion
60
+ export { isEqual as a, isEmptyObject as i, getEventValue as n, setPath as o, getPath as r, cloneValue as t };
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "solid-js": "^1.9.7"
23
23
  },
24
24
  "type": "module",
25
- "version": "0.1.0",
25
+ "version": "0.2.0",
26
26
  "peerDependenciesMeta": {
27
27
  "arktype": {
28
28
  "optional": true