Skip to content

Commit

Permalink
Merge pull request #28 from x0k/schema-analysis
Browse files Browse the repository at this point in the history
SvelteKit integration
  • Loading branch information
x0k authored Nov 15, 2024
2 parents 7a06bb6 + ed43046 commit 517f69d
Show file tree
Hide file tree
Showing 109 changed files with 4,679 additions and 823 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": [
"@changesets/changelog-github",
"@svitejs/changesets-changelog-github-compact",
{
"repo": "x0k/svelte-jsonschema-form"
}
Expand Down
5 changes: 5 additions & 0 deletions .changeset/empty-ants-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sjsf/form": minor
---

Add `parent` property to the `SchemaTraverserContext`
5 changes: 5 additions & 0 deletions .changeset/metal-stingrays-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"playground": minor
---

Add entries dumper
5 changes: 5 additions & 0 deletions .changeset/old-llamas-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sjsf/form": minor
---

Add additional property key validator
5 changes: 5 additions & 0 deletions .changeset/plenty-bees-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sjsf/ajv8-validator": patch
---

Improve error path inference for `propertyNames` error
5 changes: 5 additions & 0 deletions .changeset/soft-rivers-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"docs": minor
---

Add multiple forms guide
5 changes: 5 additions & 0 deletions .changeset/tricky-crews-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"docs": minor
---

Add SvelteKit integration page
2 changes: 1 addition & 1 deletion .github/workflows/deploy-pages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ permissions:

jobs:
deploy:
if: github.event_name == 'workflow_dispatch' || (github.event.pull_request.merged == true && github.actor == 'github-actions[bot]')
if: github.event_name == 'workflow_dispatch' || (github.event.pull_request.merged == true && github.event.pull_request.user.login == 'github-actions[bot]')
runs-on: ubuntu-latest
environment:
name: github-pages
Expand Down
36 changes: 34 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,41 @@
"request": "launch",
"name": "Debug Form Test File",
"autoAttachChildProcesses": true,
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"skipFiles": [
"<node_internals>/**",
"**/node_modules/**"
],
"program": "${workspaceRoot}/packages/form/node_modules/vitest/vitest.mjs",
"args": ["run", "${relativeFile}", "-r", "${workspaceRoot}/packages/form/"],
"args": [
"run",
"${relativeFile}",
"-r",
"${workspaceRoot}/packages/form/"
],
"smartStep": true,
"console": "integratedTerminal"
},
{
"type": "node",
"request": "launch",
"name": "Debug Form SvelteKit Integration Test File",
"autoAttachChildProcesses": true,
"skipFiles": [
"<node_internals>/**",
"**/node_modules/**"
],
"program": "${workspaceRoot}/packages/sveltekit-integration/node_modules/vitest/vitest.mjs",
"args": [
"run",
"../../${relativeFile}",
"-r",
"${workspaceRoot}/packages/sveltekit-integration/"
],
"env": {
"NODE_ENV": "development",
"VITE_TEST": "true"
},
"cwd": "${workspaceRoot}/packages/sveltekit-integration",
"smartStep": true,
"console": "integratedTerminal"
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ npm install @sjsf/form @sjsf/ajv8-validator ajv@8

```svelte
<script lang="ts">
import { useForm, SimpleForm, type Schema } from '@sjsf/form';
import { useForm2, SimpleForm, type Schema } from '@sjsf/form';
import { translation } from '@sjsf/form/translations/en';
import { theme } from '@sjsf/form/basic-theme';
import { createValidator } from "@sjsf/ajv8-validator";
Expand All @@ -40,7 +40,7 @@ npm install @sjsf/form @sjsf/ajv8-validator ajv@8
required: ["name"]
}
const form = useForm({
const form = useForm2({
...theme,
schema,
validator,
Expand Down
7 changes: 4 additions & 3 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@sjsf/form": "workspace:*",
"@sjsf/shadcn-theme": "workspace:*",
"@sjsf/skeleton-theme": "workspace:*",
"@sjsf/sveltekit": "workspace:*",
"@sjsf/zod-integration": "workspace:*",
"ajv": "catalog:",
"mermaid": "^11.4.0",
Expand All @@ -25,9 +26,9 @@
},
"devDependencies": {
"@astrojs/check": "^0.9.4",
"@astrojs/starlight": "^0.28.6",
"@astrojs/svelte": "^5.7.3",
"astro": "^4.16.10",
"@astrojs/starlight": "^0.29.0",
"@astrojs/svelte": "^6.0.1",
"astro": "^4.16.13",
"astro-vtbot": "^1.10.6",
"sharp": "^0.33.5",
"starlight-package-managers": "^0.8.0",
Expand Down
30 changes: 20 additions & 10 deletions apps/docs/src/components/custom-form.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import type { ErrorObject } from "ajv";
import { useForm, type FormAPI, type UseFormOptions } from "@sjsf/form";
import { useForm2, type FormAPI, type UseFormOptions2 } from "@sjsf/form";
import { translation } from "@sjsf/form/translations/en";
import { theme } from "@sjsf/form/basic-theme";
import { createValidator } from "@sjsf/ajv8-validator";

type Defaults = "widgets" | "components" | "validator" | "translation";

export type CustomOptions<T, E> = Omit<UseFormOptions<T, E>, Defaults> &
Partial<Pick<UseFormOptions<T, E>, Defaults>>;
export type CustomOptions<T, E> = Omit<UseFormOptions2<T, E>, Defaults> &
Partial<Pick<UseFormOptions2<T, E>, Defaults>>;

export function useCustomForm<T, E = ErrorObject>(options: CustomOptions<T, E>): FormAPI<T, E> {
export function useCustomForm<T, E = ErrorObject>(
options: CustomOptions<T, E>
): FormAPI<T, E> {
const validator = createValidator();
return useForm(
Object.setPrototypeOf(options, {
...theme,
validator,
translation,
})
const defaults: Pick<UseFormOptions2<T, ErrorObject>, Defaults> = {
...theme,
validator,
translation,
};
return useForm2(
new Proxy(options, {
get(target, p, receiver) {
if (!(p in target)) {
return defaults[p as Defaults];
}
return Reflect.get(target, p, receiver);
},
}) as UseFormOptions2<T, E>
);
}
13 changes: 11 additions & 2 deletions apps/docs/src/content/docs/_validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import {
DEFAULT_AJV_CONFIG,
} from "@sjsf/ajv8-validator";
import { isSchemaObjectValue } from "@sjsf/form/core";
import type { Schema, SchemaValue, ValidationError } from "@sjsf/form";
import {
DEFAULT_ID_PREFIX,
DEFAULT_ID_SEPARATOR,
pathToId,
type Schema,
type SchemaValue,
type ValidationError,
} from "@sjsf/form";

class StarValidator extends AjvValidator {
validateFormData(
Expand All @@ -15,7 +22,9 @@ class StarValidator extends AjvValidator {
const errors = super.validateFormData(schema, formData);
return isSchemaObjectValue(formData) && !formData["star"]
? errors.concat({
instanceId: "root_star",
instanceId: pathToId(DEFAULT_ID_PREFIX, DEFAULT_ID_SEPARATOR, [
"star",
]),
propertyTitle: "Star",
message: "I will try my best!",
error: {} as ErrorObject,
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/_with-basic.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script lang="ts">
import { useForm, SimpleForm } from "@sjsf/form";
import { useForm2, SimpleForm } from "@sjsf/form";
import { translation } from "@sjsf/form/translations/en";
import { theme } from "@sjsf/form/basic-theme";
import { schema, uiSchema, initialValue } from "./_schema";
import { validator } from "./_validator";
const form = useForm({
const form = useForm2({
...theme,
initialValue,
schema,
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/_with-daisyui.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { SimpleForm, useForm } from "@sjsf/form";
import { SimpleForm, useForm2 } from "@sjsf/form";
import { translation } from "@sjsf/form/translations/en";
import { theme } from "@sjsf/daisyui-theme";
Expand All @@ -10,7 +10,7 @@
const astro = useAstro();
const form = useForm({
const form = useForm2({
...theme,
initialValue,
schema,
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/_with-flowbite.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { SimpleForm, useForm } from "@sjsf/form";
import { SimpleForm, useForm2 } from "@sjsf/form";
import { translation } from "@sjsf/form/translations/en";
import { theme } from "@sjsf/flowbite-theme";
Expand All @@ -10,7 +10,7 @@
const astro = useAstro();
const form = useForm({
const form = useForm2({
...theme,
initialValue,
schema,
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/_with-shadcn.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { SimpleForm, useForm } from "@sjsf/form";
import { SimpleForm, useForm2 } from "@sjsf/form";
import { translation } from "@sjsf/form/translations/en";
import { theme, setThemeContext } from "@sjsf/shadcn-theme";
import { components } from "@sjsf/shadcn-theme/default";
Expand All @@ -11,7 +11,7 @@
const astro = useAstro();
const form = useForm({
const form = useForm2({
...theme,
initialValue,
schema,
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/_with-skeleton.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { SimpleForm, useForm } from "@sjsf/form";
import { SimpleForm, useForm2 } from "@sjsf/form";
import { translation } from "@sjsf/form/translations/en";
import { theme } from "@sjsf/skeleton-theme";
Expand All @@ -10,7 +10,7 @@
const astro = useAstro();
const form = useForm({
const form = useForm2({
...theme,
initialValue,
schema,
Expand Down
16 changes: 15 additions & 1 deletion apps/docs/src/content/docs/advanced/custom-form.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,24 @@ import customFormComponentCode from './_custom-form.svelte?raw';
## Hook

Even with a simple setup, resulting code is very verbose.
Therefore, after choosing a theme, validator and translation, it is convenient to create a wrapper around the `useForm`.
Therefore, after choosing a theme, validator and translation, it is convenient to create a wrapper around the `useForm2`.

<Code code={customFormCode} lang="typescript" title="custom-form.ts" />

:::note

Note that you can simplify the above code if certain restrictions are met:

- If you are sure that you will always call the `useForm2` hook with passing the object literal, you can use the following code instead of `Proxy`:

`Object.setPrototypeOf(options, defaults)`

- If you are sure you will not use getters as options, then use `Object.assign` instead of `Proxy`:

`Object.assign(defaults, options)`

:::

## Component

You also can create your own form component.
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/advanced/state-transformation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ It will be replaced with `@sjsf/form/omit-extra-data` import in the major releas
```svelte
<script lang="ts">
import { defaultMerger } from '@sjsf/form/core'
import { useForm } from "@sjsf/form";
import { useForm2 } from "@sjsf/form";
import { translation } from "@sjsf/form/translations/en";
import { theme } from "@sjsf/form/basic-theme";
import { omitExtraData2 } from "@sjsf/form/legacy-omit-extra-data";
Expand All @@ -35,7 +35,7 @@ It will be replaced with `@sjsf/form/omit-extra-data` import in the major releas
import { validator } from "./validator";
import { transform } from "./transform";
const form = useForm({
const form = useForm2({
...theme,
initialValue,
schema,
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/guides/_live-validation.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { useForm, SimpleForm, type Schema } from "@sjsf/form";
import { useForm2, SimpleForm, type Schema } from "@sjsf/form";
import { translation } from "@sjsf/form/translations/en";
import { theme } from "@sjsf/form/basic-theme";
import { createValidator } from "@sjsf/ajv8-validator";
Expand All @@ -11,7 +11,7 @@
minLength: 10,
};
const form = useForm({
const form = useForm2({
...theme,
initialValue: "initial",
schema,
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/content/docs/guides/_minimal-setup.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { type Schema, useForm, SimpleForm } from "@sjsf/form";
import { type Schema, useForm2, SimpleForm } from "@sjsf/form";
import { translation } from "@sjsf/form/translations/en";
import { theme } from "@sjsf/form/basic-theme";
import { createValidator } from "@sjsf/ajv8-validator";
Expand All @@ -11,7 +11,7 @@
title: "Simple text input",
};
const form = useForm({
const form = useForm2({
...theme,
schema,
validator,
Expand Down
19 changes: 19 additions & 0 deletions apps/docs/src/content/docs/guides/_multiple-forms-form.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts" generics="T, E">
import type { HTMLFormAttributes } from "svelte/elements";
import { FormContent, setFromContext, SubmitButton, type FormAPI, type FormContext } from '@sjsf/form';
interface Props extends HTMLFormAttributes {
form: FormAPI<T, E>;
ctx: FormContext;
}
const { form, ctx, ...rest }: Props = $props();
setFromContext(ctx)
</script>

<form use:form.enhance {...rest}>
<!-- svelte-ignore ownership_invalid_binding -->
<FormContent bind:value={form.formValue} />
<SubmitButton />
</form>
9 changes: 9 additions & 0 deletions apps/docs/src/content/docs/guides/_multiple-forms.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
import { ShadowHost } from '@/components/shadow';
import Form from './_multiple-forms.svelte'
---

<ShadowHost client:only="svelte">
<Form client:only="svelte" />
</ShadowHost>
Loading

0 comments on commit 517f69d

Please sign in to comment.