Releases: DZakh/rescript-schema
v8.2.0
What's Changed
- Add
S.enum
helper - Improve serializing by using experimental reverse schema under the hood by @DZakh in #89
Note: The S.union serializing logic changed in the release. 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.
Full Changelog: v8.1.0...v8.2.0
v8.1.0
🆕 Tag shorthand for Js/Ts api object schema
Besides passing schemas for values in S.object
, you can also pass any Js value.
const meSchema = S.object({
id: S.number,
name: "Dmitry Zakharov",
age: 23,
kind: "human" as const,
metadata: {
description: "What?? Even an object with NaN works! Yes 🔥",
money: NaN,
},
});
This is a shorthand for the advanced s.tag
and useful for discriminated unions.
// TypeScript type for reference:
// type Shape =
// | { kind: "circle"; radius: number }
// | { kind: "square"; x: number }
// | { kind: "triangle"; x: number; y: number };
const shapeSchema = S.union([
S.object({
kind: "circle" as const,
radius: S.number,
}),
S.object({
kind: "square" as const,
x: S.number,
}),
S.object({
kind: "triangle" as const,
x: S.number,
y: S.number,
}),
]);
Full Changelog: v8.0.3...v8.1.0
v8.0.3
- Fix TS type for
assert
- Fix TS type for
s.fail
- Update regular expressions used for
email
anduuid
validations. It adds support for uuidv7 validation.
Full Changelog: v8.0.2...v8.0.3
v8.0.2
- Added
S.recursive
support for JS/TS API
Full Changelog: v8.0.1...v8.0.2
v8.0.1
v8.0.0
Big clean up release
-
Added
S.assertOrRaiseWith
orschema.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 fromStrip
toStrict
- Disable NaN check for numbers
- Change the default
-
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
andS.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
- Move operations from functions to
-
Other improvements
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 ofInvalidType
- Changed default
name
ofS.literal
schema (Literal(<value>)
-><value>
) - Renamed
InvalidJsonStruct
error toInvalidJsonSchema
, since afterrescript-struct
->rescript-schema
it became misleading - Update
operation
type to be more detailed and feature it in the error message.
Full Changelog: v7.0.2...v8.0.0
v7.0.2
- Fixed critical error with infinite cycle when getting error message of InvalidType error thrown by recursive schema.
Full Changelog: v7.0.1...v7.0.2
v7.0.1
Fix regression with transformed object serializing in unions.
Full Changelog: v7.0.0...v7.0.1
v7.0.0
ReScript Schema V7 🔥
S.object
superpowers 🦸
s.flatten
Now, it's possible to spread/flatten an object schema in another object schema, allowing you to reuse schemas in a more powerful way.
type entityData = {
name: string,
age: int,
}
type entity = {
id: string,
...entityData,
}
let entityDataSchema = S.object(s => {
name: s.field("name", S.string),
age: s.field("age", S.int),
})
let entitySchema = S.object(s => {
let {name, age} = s.flatten(entityDataSchema)
{
id: s.field("id", S.string),
name,
age,
}
})
s.nestedField
A new nice way to parse nested fields:
let schema = S.object(s => {
{
id: s.field("id", S.string),
name: s.nestedField("data", "name", S.string)
age: s.nestedField("data", "name", S.int),
}
})
Object destructuring
Also, it's possible to destructure object field schemas inside of the definition. You could also notice it in the s.flatten
example 😁
let entitySchema = S.object(s => {
let {name, age} = s.field("data", entityDataSchema)
{
id: s.field("id", S.string),
name,
age,
}
})
🧠 While the example with
s.flatten
expect an object with the type{id: string, name: string, age: int}
, the example above and withs.nestedField
will expect an object with the type{id: string, data: {name: string, age: int}}
.
Extend field with another object schema
You can define object field multiple times to extend it with more fields:
let entitySchema = S.object(s => {
let {name, age} = s.field("data", entityDataSchema)
let additionalData = s.field("data", s => {
"friends": s.field("friends", S.array(S.string))
})
{
id: s.field("id", S.string),
name,
age,
friends: additionalData["friends"],
}
})
🧠 Destructuring works only with not-transformed object schemas. Be careful since it's not protected by type system.
Autocomplete improvements ⌨️
Updated context type names to s
for better auto-complete in your IDE.
effectCtx
->s
Object.ctx
->Object.s
Tuple.ctx
->Tuple.s
schemaCtx
->Schema.s
catchCtx
->Catch.s
S.json
redesign 💽
Added unsafe mode for S.json
:
S.json
->S.json(~validate: bool)
- More flexible
- Improved tree-shaking
- Tools using
rescript-schema
can get the info from thetagged
type:JSON
->JSON({validated: bool})
Other cool changes and sometimes breaking 💣
-
Added
serializeToJsonStringOrRaiseWith
-
Allow to create
S.union
with a single item -
Removed
s.failWithError
. UseS.Error.raise
instead -
PPX: Removed
@schema
for type expressions. Use@s.matches
instead. -
Removed async support for
S.union
. Please create an issue if you used the feature -
Improved parsing performance of
S.array
andS.dict
~3 times -
Automatic serializing stopped working for tuples/objects/unions of literals. Use
S.literal
instead -
Removed
InvalidTupleSize
error code in favor ofInvalidType
-
Changed payload of
Object
andTuple
variants in thetagged
type -
Redesigned
Literal
module to make it more efficient- The
S.Literal.t
type was renamed toS.literal
, became private and changed structure. UseS.Literal.parse
to create instances of the type S.Literal.classify
->S.Literal.parse
S.Literal.toText
->S.Literal.toString
. Also, started using.toString
forFunction
literals and removed spaces forDict
andArray
literals to make them look the same as theJSON.stringify
output
- The
-
Moved built-in refinements from nested modules to improve tree-shaking:
-
S.Int.min
->S.intMin
-
S.Int.max
->S.intMax
-
S.Int.port
->S.port
-
S.Float.min
->S.floatMin
-
S.Float.max
->S.floatMax
-
S.Array.min
->S.arrayMinLength
-
S.Array.max
->S.arrayMaxLength
-
S.Array.length
->S.arrayLength
-
S.String.min
->S.stringMinLength
-
S.String.max
->S.stringMaxLength
-
S.String.length
->S.stringLength
-
S.String.email
->S.email
-
S.String.uuid
->S.uuid
-
S.String.cuid
->S.cuid
-
S.String.url
->S.url
-
S.String.pattern
->S.pattern
-
S.String.datetime
->S.datetime
-
S.String.trim
->S.trim
-
S.Int.min
->S.intMin
-
S.Int.max
->S.intMax
-
S.Number.max
->S.numberMax
/S.integerMax
-
S.Number.min
->S.numberMin
/S.integerMin
-
Full Changelog: v6.4.0...v7.0.0
v6.4.0
- PPX: Add support for built-in
dict
type - PPX: Fix
@as
inside inline records in variants - Change the
rescript
peer dependency version to11.x
Full Changelog: v6.3.0...v6.4.0