Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Sep 10, 2024
1 parent b0282bf commit 7f0a9f0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/js-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ An union represents a logical OR relationship. You can apply this concept to you

The schema function `union` creates an OR relationship between any number of schemas that you pass as the first argument in the form of an array. On validation, the schema returns the result of the first schema that was successfully validated.

> 🧠 Schemas are not guaranteed to be validated in the order they are passed to `S.union`. They are grouped by the input data type to optimise performance and improve error message. Schemas with unknown data typed validated the last.
```ts
// TypeScript type for reference:
// type Union = string | number;
Expand All @@ -429,8 +431,6 @@ stringOrNumberSchema.parseOrThrow("foo"); // passes
stringOrNumberSchema.parseOrThrow(14); // passes
```

If a bad input can be uniquely assigned to one of the schemas based on the data type, the result of that schema is returned. Otherwise, a general issue is returned that contains the issues of each schema as subissues.

### Discriminated unions

```typescript
Expand Down
8 changes: 6 additions & 2 deletions docs/rescript-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,12 @@ Circle({radius: 1})->S.serializeWith(schema)

`array<S.t<'value>> => S.t<'value>`

An union represents a logical OR relationship. You can apply this concept to your schemas with `S.union`. This is the best API to use for variants and polymorphic variants.

On validation, the `S.union` schema returns the result of the first item that was successfully validated.

> 🧠 Schemas are not guaranteed to be validated in the order they are passed to `S.union`. They are grouped by the input data type to optimise performance and improve error message. Schemas with unknown data typed validated the last.
```rescript
// TypeScript type for reference:
// type Shape =
Expand Down Expand Up @@ -751,8 +757,6 @@ Square({x: 2.})->S.serializeWith(shapeSchema)
// })
```

The `union` will test the input against each of the schemas in order and return the first value that validates successfully.

#### Enums

Also, you can describe enums using `S.union` together with `S.literal`.
Expand Down

0 comments on commit 7f0a9f0

Please sign in to comment.