Skip to content

Commit

Permalink
Document S.shape
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Feb 18, 2025
1 parent 14d391e commit 680ebd4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 24 additions & 4 deletions docs/js-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Use the `S.strip` function to reset an object schema to the default behavior (st
Both `S.strict` and `S.strip` are applied for the first level of the object schema. If you want to apply it for all nested schemas, you can use `S.deepStrict` and `S.deepStrip` functions.

```ts
let schema = S.schema({
const schema = S.schema({
bar: {
baz: S.string,
},
Expand Down Expand Up @@ -649,7 +649,7 @@ type Node = {
children: Node[];
};

let nodeSchema = S.recursive<Node>((nodeSchema) =>
const nodeSchema = S.recursive<Node>((nodeSchema) =>
S.schema({
id: S.string,
children: S.array(nodeSchema),
Expand Down Expand Up @@ -700,7 +700,7 @@ await S.parseAsyncOrThrow(

## Transforms

**rescript-schema** allows to augment schema with transformation logic, letting you transform value during parsing and serializing. This is most commonly used for mapping value to more convenient data-structures.
**rescript-schema** allows to augment schema with transformation logic, letting you transform value during parsing and serializing. This is most commonly used when you need to access the value in runtime and perform some transformation logic. For cases when you only want to change the shape of the data, it's better to use `S.shape` instead.

```ts
const intToString = (schema) =>
Expand All @@ -717,6 +717,26 @@ const intToString = (schema) =>
);
```

> 🧠 You can use `S.coerce(S.int, S.string)` which is a better version of the above example.
### **`shape`**

The `S.shape` schema is a helper function that allows you to transform the value to a desired shape. It'll statically derive required data transformations to perform the change in the most optimal way.

> ⚠️ Even though it looks like you operate with a real value, it's actually a dummy proxy object. So conditions or any other runtime logic won't work. Please use `S.transform` for such cases.
```typescript
const circleSchema = S.shape(S.number, (radius) => ({
kind: "circle",
radius: radius,
}));

S.parseOrThrow(1, circleSchema); //? { kind: "circle", radius: 1 }

// Also works in reverse 🔄
S.reverseConvertOrThrow({ kind: "circle", radius: 1 }, circleSchema); //? 1
```

## Functions on schema

### Built-in operations
Expand Down Expand Up @@ -918,7 +938,7 @@ Or the async version:

```ts
const result = await S.safeAsync(async () => {
let passed = await S.parseAsyncOrThrow(data, S.schema(S.boolean));
const passed = await S.parseAsyncOrThrow(data, S.schema(S.boolean));
return passed ? 1 : 0;
});
```
Expand Down
4 changes: 4 additions & 0 deletions docs/rescript-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,10 @@ let tupleExampleSchema = S.schema(s => (#id, s.matches(S.string)))

`(S.t<'value>, 'value => 'shape) => S.t<'shape>`

The `S.shape` schema is a helper function that allows you to transform the value to a desired shape. It'll statically derive required data transformations to perform the change in the most optimal way.

> ⚠️ Even though it looks like you operate with a real value, it's actually a dummy proxy object. So conditions or any other runtime logic won't work. Please use `S.transform` for such cases.
```rescript
type shape = Circle({radius: float}) | Square({x: float}) | Triangle({x: float, y: float})
Expand Down

1 comment on commit 680ebd4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 680ebd4 Previous: 5d4c24f Ratio
S.schema - make 1335514 ops/sec (±1.20%) 1333035 ops/sec (±0.94%) 1.00
S.schema - make + parse 106121 ops/sec (±0.58%) 107810 ops/sec (±1.18%) 1.02
S.schema - parse 48959926 ops/sec (±1.59%) 49514611 ops/sec (±1.45%) 1.01
S.schema - parse strict 23201356 ops/sec (±1.11%) 23185793 ops/sec (±1.21%) 1.00
S.schema - make + reverse 915128 ops/sec (±0.57%) 916551 ops/sec (±0.35%) 1.00
S.schema - make + reverse convert 179556 ops/sec (±0.71%) 178335 ops/sec (±1.39%) 0.99
S.schema - reverse convert 71912044 ops/sec (±2.51%) 56582336 ops/sec (±1.79%) 0.79
S.schema - reverse convert (compiled) 133604493 ops/sec (±5.29%) 136263821 ops/sec (±4.28%) 1.02
S.schema - assert 50445388 ops/sec (±1.71%) 51172085 ops/sec (±2.32%) 1.01
S.schema - assert (compiled) 72015653 ops/sec (±2.47%) 72465443 ops/sec (±2.31%) 1.01
S.schema - assert strict 21915661 ops/sec (±1.04%) 21906524 ops/sec (±0.73%) 1.00
S.object - make 993736 ops/sec (±0.17%) 965712 ops/sec (±0.36%) 0.97
S.object - make + parse 86932 ops/sec (±0.40%) 89541 ops/sec (±0.22%) 1.03
S.object - parse 33448836 ops/sec (±1.38%) 33576635 ops/sec (±1.02%) 1.00
S.object - make + reverse 124612 ops/sec (±1.32%) 128955 ops/sec (±1.24%) 1.03
S.object - make + reverse convert 79687 ops/sec (±1.14%) 83466 ops/sec (±1.10%) 1.05
S.object - reverse convert 44935347 ops/sec (±2.47%) 46593380 ops/sec (±1.70%) 1.04
S.string - parse 65482187 ops/sec (±2.42%) 63773163 ops/sec (±2.49%) 0.97
S.string - reverse convert 67915533 ops/sec (±2.34%) 67690417 ops/sec (±2.11%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.