Skip to content

Commit 891db75

Browse files
committed
add high level architecture overview into CONTRIBUTING.md
1 parent 02223bf commit 891db75

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

CONTRIBUTING.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ If you have questions about the library, found a bug or want to suggest a featur
66

77
## Documentation
88

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.
1010

1111
## Setup
1212

@@ -94,7 +94,7 @@ pnpm test:file path/to/file
9494

9595
#### Local build
9696

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.
9898

9999
Once you have a `dist` folder being created, you can either:
100100
- Option A: Point your local project import to the `dist` folder.
@@ -179,3 +179,54 @@ The final release is done after merge.
179179
1. Choose the tag matching the newest version.
180180
2. Leave the title empty
181181
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.
190+
191+
### Main entry point
192+
193+
- `src/index.ts` re-exports `createHeadlessForm` & `modifySchema`
194+
- `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

Comments
 (0)