Skip to content

Commit 94b19cf

Browse files
authored
Merge pull request #41 from x0k/structure-change
Implement array and object fields revalidation
2 parents 871e2d6 + f859158 commit 94b19cf

35 files changed

+386
-214
lines changed

.changeset/quick-coins-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sjsf/form": minor
3+
---
4+
5+
Implement array and object fields revalidation

.changeset/twenty-mirrors-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"playground": minor
3+
---
4+
5+
Allow to customize validation events and modifiers
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Fields validation mode
3+
sidebar:
4+
order: 11
5+
---
6+
7+
```typescript
8+
type FieldsValidationMode = number;
9+
10+
/** Validation is triggered on input event */
11+
export const ON_INPUT = 1;
12+
/** Validation is triggered on change event */
13+
export const ON_CHANGE = ON_INPUT << 1;
14+
/** Validation is triggered on blur event */
15+
export const ON_BLUR = ON_CHANGE << 1;
16+
/** Validation is triggered on add/remove item in array */
17+
export const ON_ARRAY_CHANGE = ON_BLUR << 1;
18+
/** Validation is triggered on add/remove/rename property in object */
19+
export const ON_OBJECT_CHANGE = ON_ARRAY_CHANGE << 1;
20+
21+
/** Validation is not triggered before first change event */
22+
export const AFTER_CHANGED = ON_OBJECT_CHANGE << 1;
23+
/** Validation is not triggered before first blur event */
24+
export const AFTER_TOUCHED = AFTER_CHANGED << 1;
25+
/** Validation is not triggered before first form submission */
26+
export const AFTER_SUBMITTED = AFTER_TOUCHED << 1;

apps/docs/src/content/docs/api-reference/handlers.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ interface FormProps<T> {
4949
/**
5050
* Field validation error handler
5151
*/
52-
onFieldValidationFailure?: (
52+
onFieldsValidationFailure?: (
5353
state: FailedMutation<unknown>,
5454
config: Config,
5555
value: SchemaValue | undefined

apps/docs/src/content/docs/api-reference/inputs-validation-mode.mdx

Lines changed: 0 additions & 23 deletions
This file was deleted.

apps/docs/src/content/docs/guides/_async-validation.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
validator: createAsyncValidator({ ajv }),
3737
handleValidationProcessError,
3838
schema: schema,
39-
inputsValidationMode: ON_INPUT,
39+
fieldsValidationMode: ON_INPUT,
4040
onSubmit: console.log,
4141
});
4242
@@ -48,7 +48,7 @@
4848
};
4949
</script>
5050

51-
form validation: {statusNames[form.validation.status]}, inputs validation: {statusNames[form.fieldValidation.status]}
51+
form validation: {statusNames[form.validation.status]}, fields validation: {statusNames[form.fieldsValidation.status]}
5252
<SimpleForm
5353
{form}
5454
novalidate
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
import { ShadowHost } from "@/components/shadow";
3+
4+
import Form from "./_fields-validation.svelte";
5+
---
6+
7+
<ShadowHost client:only="svelte">
8+
<Form client:only="svelte" />
9+
</ShadowHost>

apps/docs/src/content/docs/guides/_inputs-validation.svelte renamed to apps/docs/src/content/docs/guides/_fields-validation.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
const form = useCustomForm({
99
schema: objectSchema,
10-
inputsValidationMode: ON_INPUT | ON_CHANGE | AFTER_SUBMITTED,
10+
fieldsValidationMode: ON_INPUT | ON_CHANGE | AFTER_SUBMITTED,
1111
onSubmit: console.log,
1212
});
1313
</script>

apps/docs/src/content/docs/guides/_inputs-validation.astro

Lines changed: 0 additions & 9 deletions
This file was deleted.

apps/docs/src/content/docs/guides/validation.mdx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ sidebar:
66

77
import { Code, Card, LinkCard } from '@astrojs/starlight/components';
88

9-
import { DEFAULT_FIELD_VALIDATION_DEBOUNCE_MS } from '@sjsf/form';
9+
import { DEFAULT_FIELDS_VALIDATION_DEBOUNCE_MS } from '@sjsf/form';
1010

1111
import LiveValidation from './_live-validation.astro'
1212
import liveValidationCode from './_live-validation.svelte?raw'
1313

14-
import InputsValidation from './_inputs-validation.astro'
15-
import inputsValidationCode from './_inputs-validation.svelte?raw'
14+
import FieldsValidation from './_fields-validation.astro'
15+
import fieldsValidationCode from './_fields-validation.svelte?raw'
1616

1717
import AsyncValidation from './_async-validation.astro'
1818
import asyncValidationCode from './_async-validation.svelte?raw'
@@ -44,27 +44,28 @@ By utilizing Svelte 5 reactivity, we can easily implement live validation.
4444
While it is possible, this approach has low efficiency, because usually
4545
revalidation of the whole form when changing one field does not make sense.
4646

47-
## Inputs validation
47+
## Fields validation
4848

49-
Instead of full form revalidation, we propose to perform revalidation of only the input field and full validation of the form when submitting.
49+
Instead of full form revalidation, we propose to perform revalidation of only the changing field and
50+
full validation of the form when submitting.
5051

5152
:::note
5253

53-
By default, inputs validation is performed with a debounce of {DEFAULT_FIELD_VALIDATION_DEBOUNCE_MS}ms.
54+
By default, fields validation is performed with a debounce of {DEFAULT_FIELDS_VALIDATION_DEBOUNCE_MS}ms.
5455

55-
This can be changed by passing the `validationDebounceMs` option.
56+
This can be changed by passing the `fieldsValidationDebounceMs` option.
5657

5758
:::
5859

59-
<Code code={inputsValidationCode} lang="svelte" />
60+
<Code code={fieldsValidationCode} lang="svelte" />
6061

6162
<Card>
62-
<InputsValidation />
63+
<FieldsValidation />
6364
</Card>
6465

6566
The form in this example will only revalidate input fields on the `input` and `change` events after the first submission of the form.
6667

67-
<LinkCard title="Inputs validation mode API reference" href="../../api-reference/inputs-validation-mode/" />
68+
<LinkCard title="Fields validation mode API reference" href="../../api-reference/fields-validation-mode/" />
6869

6970
## Async validation
7071

@@ -75,7 +76,7 @@ The form supports asynchronous validation, please see your validator page for mo
7576
By default, a new form validation process can only be started after the previous one is completed;
7677
a new input validation process aborts the previous one.
7778

78-
You can change this behavior by passing the `validationCombinator` and `fieldValidationCombinator` options.
79+
You can change this behavior by passing the `validationCombinator` and `fieldsValidationCombinator` options.
7980

8081
:::
8182

0 commit comments

Comments
 (0)