Skip to content

Commit

Permalink
Merge pull request #52 from esafak/refine
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte authored Sep 29, 2023
2 parents a50be6f + 51ab7e1 commit 09a3854
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-eggs-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"formsnap": patch
---

Add FormValidation to Field.config.schema's type
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_modules
/dist
/.svelte-kit
/package
.cache
.env
.env.*
!.env.example
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
],
"peerDependencies": {
"svelte": "^4.0.0",
"sveltekit-superforms": "^1.6.1",
"sveltekit-superforms": "^1.7.1",
"zod": "^3.22.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/form-description.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { getFormField } from "@/lib/index.js";
import type { DescriptionProps } from "../types.js";
import type { DescriptionProps } from "$lib";
type $$Props = DescriptionProps;
export let tag = "p";
Expand Down
5 changes: 3 additions & 2 deletions src/lib/components/form-field.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script lang="ts">
import { createFormField, type Form, type FormFieldName } from "@/lib/internal/index.js";
import { createFormField } from "@/lib/internal/index.js";
import type { Form, FormFieldName, FormValidation} from "@/lib/internal/index.js";
import type { AnyZodObject } from "zod";
type T = $$Generic<AnyZodObject>;
type T = $$Generic<AnyZodObject | FormValidation>;
type Path = $$Generic<FormFieldName<T>>;
export let config: Form<T>;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/form.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts" context="module">
import type { ZodValidation } from "sveltekit-superforms";
import type { AnyZodObject } from "zod";
import type { Form } from "@/lib/internal/index.js";
type Validation = ZodValidation<AnyZodObject>;
</script>
Expand Down Expand Up @@ -64,7 +65,7 @@
capture
} = superFrm;
const config = {
const config: Form<T> = {
form: superFrm,
schema
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/internal/form-field/create-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createFormField<

const setValue = (v: unknown) => {
//@ts-expect-error - do we leave this as is, or do we want to force the type to match the schema?
// Pros: we don't have to deal with type narrowing inside the `setValue` function and we're runtime validating the type with zod anyways.
// Pros: we don't have to deal with type narrowing inside the `setValue` function, and we're runtime validating the type with zod anyways.
// Cons: we're not forcing the type to match the schema so more issues could occur.
value.set(v);
};
Expand Down Expand Up @@ -65,7 +65,7 @@ export function createFormField<
return {
"aria-invalid": errors ? true : undefined,
"aria-describedby": describedBy,
"aria-required": constraints?.required ? true : false,
"aria-required": !!constraints?.required,
"data-invalid": errors ? true : undefined,
"data-valid": errors ? undefined : true,
name,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type ExpandDeep<T> = T extends object

export type Form<T extends FormValidation> = {
schema: T;
form: SuperForm<T, unknown>;
form: SuperForm<T>;
};

export type Arrayable<T> = T | T[];
Expand Down
21 changes: 21 additions & 0 deletions tests/RefinedValidation.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script lang="ts">
import { Root, Field } from "$lib/components";
import type { SuperValidated } from "sveltekit-superforms";
import { z } from "zod";
export const schema = z.object({
name: z.string().min(1),
secondName: z.string().min(1)
})
.refine(({ name, secondName }) => name === secondName, {
message: 'Names must be the same',
path: ['secondName']
})
;
export let form: SuperValidated<typeof schema>;
</script>

<Root method="POST" {form} {schema} let:config>
<Field {config} name="name" />
<Field {config} name="secondName" />
</Root>

0 comments on commit 09a3854

Please sign in to comment.