-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
201 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,39 @@ | ||
# Changelog | ||
|
||
- Allow object destructuring in S.variant | ||
- Improve peformance of running serialization to JSON or JSON string for the first time | ||
- Fix serialization to JSON or JSON string for nested recursive schema | ||
- S.jsonString doesn't fail on getting non-jsonable schema anymore. It will fail on the first serialization run instead | ||
- When serializing to JSON or JSON string the S.union now tries to serialize other items when encounters a non-jsonable schema. Before it used to fail the whole union. | ||
- For JS/TS users | ||
- Move operations from functions to Schema methods | ||
- Add `serializeToJsonOrThrow` | ||
- Update operation type to be more detailed and feature it in the error message. | ||
- S.union supports schemas with async | ||
- Added `S.assertOrRaiseWith` or `schema.assert` for js/ts users. It doesn't return parsed value, but that makes the function 2-3 times faster, depending on the schema. | ||
- Improved S.recursive implementation | ||
- Added `S.setGlobalConfig`. Now it's possible to customize the behavior of the library: | ||
# Big clean up release | ||
|
||
- Added `S.assertOrRaiseWith` or `schema.assert` for JS/TS users. It doesn't return parsed value, which makes the operation 2-3 times faster for some schemas. | ||
- Added `S.setGlobalConfig`. Now it's possible to customize the global behavior of the library: | ||
|
||
- Change the default `unknownKeys` strategy for Object from `Strip` to `Strict` | ||
- Disable NaN check for numbers | ||
- Removed `parseAsyncInStepsWith` and `parseAnyAsyncInStepsWith` to reduce internal library complexity. Let me know if you need it. I can re-implement it in a future version in a simpler way. | ||
- Refactored parse async. Fixed some bugs and made it more performant. | ||
- Improved performance and errors of S.union schema | ||
- Removed `InvalidLiteral` error in favor of `InvalidType` | ||
- Changed default `name` of `S.literal` schema. `Literal(<value>)` is now `<value>` | ||
- Renamed `InvalidJsonStruct` error, since after `rescript-struct`->`rescript-schema` it became misleading | ||
- Improve TS types and add interop with genType | ||
|
||
// TODO: | ||
- `S.union` refactoring | ||
- Drastically improved parsing performance (1x-1000x times faster depending on the case) | ||
- Returned back async support | ||
- More specific error messages | ||
- When serializing to JSON or JSON string the `S.union` now tries to serialize other items when encounters a non-jsonable schema. Before it used to fail the whole union. | ||
- Parse Async refactoring | ||
- Performance improvements | ||
- Made it more maintainable and less error-prone | ||
- Hidden bug fixes | ||
- Removed `S.parseAsyncInStepsWith` and `S.parseAnyAsyncInStepsWith` to reduce internal library complexity. Create an issue if you need it. | ||
- `S.recursive` refactoring | ||
|
||
- Performance improvements | ||
- Made it more maintainable and less error-prone | ||
- Fixed bug with serializing to JSON or JSON string | ||
|
||
- For JS/TS users | ||
|
||
- Move operations from functions to `Schema` methods | ||
- Add `serializeToJsonOrThrow` | ||
- Improve TS types and make them compatible with generated types from `genType` | ||
|
||
- Other improvements | ||
|
||
- Set 8.0.x for rescript-schema in ppx deps | ||
- `S.jsonString` doesn't fail on getting non-jsonable schema anymore. It will fail on the first serialization run instead | ||
- Removed `InvalidLiteral` error in favor of `InvalidType` | ||
- Changed default `name` of `S.literal` schema (`Literal(<value>)` -> `<value>`) | ||
- Renamed `InvalidJsonStruct` error to `InvalidJsonSchema`, since after `rescript-struct` -> `rescript-schema` it became misleading | ||
- Update `operation` type to be more detailed and feature it in the error message. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,10 +72,10 @@ const loginSchema = S.object({ | |
type LoginData = S.Output<typeof loginSchema>; // { email: string; password: string } | ||
|
||
// Throws the S.Error(`Failed parsing at ["email"]. Reason: Invalid email address`) | ||
S.parseOrThrow(loginSchema, { email: "", password: "" }); | ||
loginSchema.parseOrThrow({ email: "", password: "" }); | ||
|
||
// Returns data as { email: string; password: string } | ||
S.parseOrThrow(loginSchema, { | ||
loginSchema.parseOrThrow({ | ||
email: "[email protected]", | ||
password: "12345678", | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,26 +2,25 @@ | |
[![codecov](https://codecov.io/gh/DZakh/rescript-schema/branch/main/graph/badge.svg?token=40G6YKKD6J)](https://codecov.io/gh/DZakh/rescript-schema) | ||
[![npm](https://img.shields.io/npm/dm/rescript-schema)](https://www.npmjs.com/package/rescript-schema) | ||
|
||
# ReScript Schema | ||
# ReScript Schema 🧬 | ||
|
||
The fastest composable parser/serializer for ReScript and TypeScript | ||
The fastest parser in the entire JavaScript ecosystem with a focus on small bundle size and top-notch DX. | ||
|
||
> ⚠️ Be aware that **rescript-schema** uses `eval` for parsing. It's usually fine but might not work in some environments like Cloudflare Workers or third-party scripts used on pages with the [script-src](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src) header. | ||
Highlights: | ||
|
||
- Combines validation and transformation without a performance loss | ||
- Can transform parsed value back to the initial format (serializing) | ||
- Works with any Js value, not only `Js.Json.t` | ||
- Works with plain JavaScript, TypeScript, and ReScript. You don't need to use any compiler. | ||
- The **fastest** parsing and validation library in the entire JavaScript ecosystem ([benchmark](https://moltar.github.io/typescript-runtime-type-benchmarks/)) | ||
- Small JS footprint & tree-shakable API ([Comparison with Zod and Valibot](#comparison)) | ||
- Describe transformations in a schema without a performance loss | ||
- Can reverse transformed values to the initial format (serializing) | ||
- Error messages are detailed and easy to understand | ||
- Support for asynchronous transformations | ||
- Immutable API with both result and exception-based operations | ||
- Easy to create _recursive_ schema | ||
- Detailed error messages | ||
- Opt-in strict mode for object schema to prevent excessive fields and many more built-in helpers | ||
- Opt-in PPX to generate schema from type | ||
- Works with plain JavaScript/TypeScript too! You don't need to use ReScript | ||
- The **fastest** composable validation library in the entire JavaScript ecosystem ([benchmark](https://moltar.github.io/typescript-runtime-type-benchmarks/)) | ||
- Small JS footprint & tree-shakable API ([Comparison with Zod and Valibot](#comparison)) | ||
- Opt-in strict mode for object schema to prevent excessive fields. And many more built-in helpers | ||
- Opt-in ReScript PPX to generate schema from type definition | ||
|
||
Also, it has declarative API allowing you to use **rescript-schema** as a building block for other tools, such as: | ||
|
||
|
@@ -47,12 +46,12 @@ Besides the individual bundle size, the overall size of the library is also sign | |
|
||
At the same time **rescript-schema** is the fastest composable validation library in the entire JavaScript ecosystem. This is achieved because of the JIT approach when an ultra optimized validator is created using `eval`. | ||
|
||
| | rescript-schema@7.0.0 | [email protected] | Valibot@0.32.0 | | ||
| | rescript-schema@8.0.0 | [email protected] | Valibot@0.36.0 | | ||
| ---------------------------------------- | --------------------- | --------------- | -------------- | | ||
| **Total size** (minified + gzipped) | 9.82 kB | 14.6 kB | 9.88 kB | | ||
| **Example size** (minified + gzipped) | 4.98 kB | 12.9 kB | 1.22 B | | ||
| **Nested object parsing** | 156,244 ops/ms | 1,304 ops/ms | 3,822 ops/ms | | ||
| **Create schema + parse once** | 56 ops/ms | 112 ops/ms | 2,475 ops/ms | | ||
| **Total size** (minified + gzipped) | 9.71 kB | 14.6 kB | 10.4 kB | | ||
| **Example size** (minified + gzipped) | 5.48 kB | 12.9 kB | 1.22 B | | ||
| **Parse with the same schema** | 155,887 ops/ms | 1,273 ops/ms | 3,771 ops/ms | | ||
| **Create schema & parse once** | 146 ops/ms | 108 ops/ms | 2,485 ops/ms | | ||
| **Eval-free** | ❌ | ✅ | ✅ | | ||
| **Codegen-free** (Doesn't need compiler) | ✅ | ✅ | ✅ | | ||
| **Ecosystem** | ⭐️ | ⭐️⭐️⭐️⭐️⭐️ | ⭐️⭐️⭐️ | | ||
| **Ecosystem** | ⭐️⭐️ | ⭐️⭐️⭐️⭐️⭐️ | ⭐️⭐️⭐️ | |
Oops, something went wrong.
96d1a90
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
Parse string
819487356
ops/sec (±0.12%
)818372259
ops/sec (±0.13%
)1.00
Serialize string
820530198
ops/sec (±0.07%
)819110535
ops/sec (±0.05%
)1.00
Advanced object schema factory
470647
ops/sec (±0.67%
)449156
ops/sec (±0.56%
)0.95
Parse advanced object
56966512
ops/sec (±0.57%
)45100200
ops/sec (±0.16%
)0.79
Assert advanced object
173510719
ops/sec (±0.40%
)Create and parse advanced object
93492
ops/sec (±0.23%
)35041
ops/sec (±1.18%
)0.37
Parse advanced strict object
25424760
ops/sec (±0.24%
)22748126
ops/sec (±0.24%
)0.89
Assert advanced strict object
30740944
ops/sec (±0.61%
)Serialize advanced object
75366763
ops/sec (±0.18%
)806595235
ops/sec (±0.11%
)10.70
This comment was automatically generated by workflow using github-action-benchmark.
96d1a90
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
1.50
.Serialize advanced object
75366763
ops/sec (±0.18%
)806595235
ops/sec (±0.11%
)10.70
This comment was automatically generated by workflow using github-action-benchmark.