You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CONTRIBUTING.md
+53-2Lines changed: 53 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ If you have questions about the library, found a bug or want to suggest a featur
6
6
7
7
## Documentation
8
8
9
-
Documentation website is available [here](https://json-schema-form.vercel.app/). Please note that its source code is not in the repo yet. Our docs are still coupled to Remote's internal Design System and integration tests. The effort to decouple it at the moment is too high.
9
+
Documentation website is available [here](https://json-schema-form.vercel.app/). Please note that its source code is not in the repo yet. Our docs are still coupled to Remote's internal Design System and integration tests. The effort to decouple it at the moment is too high. Please refer to [this section](#how-it-works) for details about how the library works.
10
10
11
11
## Setup
12
12
@@ -94,7 +94,7 @@ pnpm test:file path/to/file
94
94
95
95
#### Local build
96
96
97
-
The simplest way to test your local changes is to run the `dev` script — this re-generates a `dist` folder whenever a file is changed.
97
+
The simplest way to test your local changes is to run the `dev` script — this re-generates a `dist` folder whenever a file is changed.
98
98
99
99
Once you have a `dist` folder being created, you can either:
100
100
- Option A: Point your local project import to the `dist` folder.
@@ -179,3 +179,54 @@ The final release is done after merge.
179
179
1. Choose the tag matching the newest version.
180
180
2. Leave the title empty
181
181
3. Copy the new part of the CHANGELOG to the description.
182
+
183
+
## How it works
184
+
185
+
This is a high level overview of how the library works and what its main components are.
186
+
187
+
## Architecture Overview
188
+
189
+
The code is organized into clear, focused modules to provide a clear separation of concerns and allow us to unit test each individual part of the library.
- `src/form.ts` defines `createHeadlessForm` the main entry point to the library and orchestrates **field generation** and **validation**
195
+
196
+
### Field generation
197
+
198
+
- `src/field/schema.ts` implements generation of fields that can be used to build user interfaces from
199
+
- `buildFieldSchema` is the main function that builds a field for any given schema, we assume that the root schema is always of type `object`
200
+
- The rest of this modules implements helper functions for mapping the different schema types to corresponding fields
201
+
202
+
### Validation Logic
203
+
204
+
- `src/validation/schema.ts` provides the `validateSchema` function which can validate a JSON value against a given schema and returns validation errors in the form of `ValidationError[]`
205
+
- `validateSchema` calls all type or keyword specific validation functions such as `validateObject`, `validateArray`, `validateString`, `validateAnyOf` , `validateCondition`, etc. and combine their returned validation errors
206
+
- Those validation functions apply their validation logic when needed or simply return `[]`
207
+
- When those functions need to validate a nested schema or `subschema` as the spec calls it, they recursively call `validateSchema`
208
+
209
+
### Validation Error Handling
210
+
211
+
- `src/errors/index.ts` defines the `ValidationError` type return from the validation functions
212
+
- `src/errors/messages.ts` defines `getErrorMessage` which translates validation errors into human readable error messages which can be shown in a UI, these error messages are returned when calling `handleValidation` on a form as part of the `ValidationResult`
213
+
214
+
## Configuration options
215
+
216
+
`createHeadlessForm` takes an options object with three properties: [CreateHeadlessFormOptions](https://github.com/remoteoss/json-schema-form/blob/main/next/src/form.ts)
217
+
218
+
- `initialValues` values to be used when initially rendering a form
219
+
- `strictInputType` when true, each `['x-jsf-presentation'].inputType` must be present for each schema property
220
+
- `validationOptions` an object that specifies options specific to validation, these options are here mainly for dragon and are disabled by default
221
+
- `treatNullAsUndefined`
222
+
- when true, a `null` value will be considered absent when validated against a schema
223
+
- per the json-schema spec `null` is a perfectly valid value that can be validated, for example with a schema like `{ "type": "null" }` that only allows a `null` value
224
+
- providing a `null` value to a schema like `{ "type": "string" }` should result in a validation error as the value’s type does not match
225
+
- json-schema-form v0 does not return this error and all our forms rely on this, form fields that have not been filled, even hidden ones, will cause `null` values to be passed which would result in validation errors in nearly every form
226
+
- dragon’s `useCreateHeadlessForm` hook enables this option by default
227
+
- `allowForbiddenValues`
228
+
- when true, validating a value against a `false` schema will not result in a validation error
229
+
- when validating any value against a schema that is `false` the json-schema spec says that this is a validation error, as `false` means “you must not provide a value to this schema”
230
+
- we frequently use `false` schemas when we conditionally hide properties by setting them to `false`
231
+
- as mentioned above, dragon passes `null` values for hidden fields to `handleValidate`
232
+
- dragon’s `useCreateHeadlessForm` hook enables this option by default
0 commit comments