Skip to content

Commit

Permalink
README: Recommend using the strict: true option to get better error… (
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jun 30, 2024
1 parent 34faeb6 commit 8513d7e
Show file tree
Hide file tree
Showing 21 changed files with 156 additions and 53 deletions.
6 changes: 5 additions & 1 deletion packages/experimental/src/DevTools/Domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ export const Gauge = metric(
const numberOrInfinity = Schema.transform(
Schema.Union(Schema.Number, Schema.Null),
Schema.Number,
{ decode: (i) => i === null ? Number.POSITIVE_INFINITY : i, encode: (i) => Number.isFinite(i) ? i : null }
{
strict: true,
decode: (i) => i === null ? Number.POSITIVE_INFINITY : i,
encode: (i) => Number.isFinite(i) ? i : null
}
)

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/src/Headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const schema: Schema.Schema<Headers, Record.ReadonlyRecord<string, string
.transform(
Schema.Record(Schema.String, Schema.Union(Schema.String, Schema.Array(Schema.String))),
schemaFromSelf,
{ decode: (record) => fromInput(record), encode: identity }
{ strict: true, decode: (record) => fromInput(record), encode: identity }
)

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/src/Transferable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const schema: {
Schema.transformOrFail(
Schema.encodedSchema(self),
self,
{ decode: ParseResult.succeed, encode: (i) => Effect.as(addAll(f(i)), i) }
{ strict: true, decode: ParseResult.succeed, encode: (i) => Effect.as(addAll(f(i)), i) }
))

/**
Expand Down
1 change: 1 addition & 0 deletions packages/platform/src/internal/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const SingleFileSchema: Schema.transform<
Schema.Schema<ReadonlyArray<Multipart.PersistedFile>>,
Schema.Schema<Multipart.PersistedFile>
> = Schema.transform(FilesSchema.pipe(Schema.itemsCount(1)), FileSchema, {
strict: true,
decode: ([file]) => file,
encode: (file) => [file]
})
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const toHandler = <R extends Router<any, any>>(router: R, options?: {
Schema.transform(
rpc.schema,
Schema.typeSchema(Schema.Tuple(rpc.schema, Schema.Any)),
{ decode: (request) => [request, rpc] as const, encode: ([request]) => request }
{ strict: true, decode: (request) => [request, rpc] as const, encode: ([request]) => request }
)
)
)
Expand Down Expand Up @@ -305,7 +305,7 @@ export const toHandlerEffect = <R extends Router<any, any>>(router: R, options?:
Schema.transform(
rpc.schema,
Schema.typeSchema(Schema.Tuple(rpc.schema, Schema.Any)),
{ decode: (request) => [request, rpc] as const, encode: ([request]) => request }
{ strict: true, decode: (request) => [request, rpc] as const, encode: ([request]) => request }
)
)
)
Expand Down Expand Up @@ -376,7 +376,7 @@ export const toHandlerRaw = <R extends Router<any, any>>(router: R) => {
Schema.transform(
Schema.typeSchema(rpc.schema),
Schema.typeSchema(Schema.Tuple(rpc.schema, Schema.Any)),
{ decode: (request) => [request, rpc] as const, encode: ([request]) => request }
{ strict: true, decode: (request) => [request, rpc] as const, encode: ([request]) => request }
)
))
const parse = Schema.decode(schema)
Expand Down
20 changes: 19 additions & 1 deletion packages/schema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ const Person = Schema.Struct({
})

const asyncSchema = Schema.transformOrFail(PersonId, Person, {
strict: true,
// Simulate an async transformation
decode: (id) =>
Effect.succeed({ id, name: "name", age: 18 }).pipe(
Expand Down Expand Up @@ -657,6 +658,7 @@ import { Effect } from "effect"
const effectify = (duration: Duration.DurationInput) =>
Schema.Number.pipe(
Schema.transformOrFail(Schema.Number, {
strict: true,
decode: (x) =>
Effect.sleep(duration).pipe(Effect.andThen(ParseResult.succeed(x))),
encode: ParseResult.succeed
Expand Down Expand Up @@ -1301,6 +1303,7 @@ const schema = Schema.transform(
Schema.String,
Schema.String.pipe(Schema.trimmed(), Schema.lowercased()),
{
strict: true,
decode: (s) => s.trim().toLowerCase(),
encode: (s) => s
}
Expand Down Expand Up @@ -2592,6 +2595,7 @@ const DiscriminatedShape = Schema.Union(
Schema.transform(
Schema.Struct({ ...Circle.fields, kind: Schema.Literal("circle") }), // Add a "kind" property with the literal value "circle" to Circle
{
strict: true,
decode: (circle) => ({ ...circle, kind: "circle" as const }), // Add the discriminant property to Circle
encode: ({ kind: _kind, ...rest }) => rest // Remove the discriminant property
}
Expand All @@ -2601,6 +2605,7 @@ const DiscriminatedShape = Schema.Union(
Schema.transform(
Schema.Struct({ ...Square.fields, kind: Schema.Literal("square") }), // Add a "kind" property with the literal value "square" to Square
{
strict: true,
decode: (square) => ({ ...square, kind: "square" as const }), // Add the discriminant property to Square
encode: ({ kind: _kind, ...rest }) => rest // Remove the discriminant property
}
Expand Down Expand Up @@ -4457,6 +4462,7 @@ export const transformedSchema = Schema.transform(
Schema.Number, // Source schema
Schema.Number, // Target schema
{
strict: true, // optional but you get better error messages from TypeScript
decode: (n) => n * 2, // Transformation function to double the number
encode: (n) => n / 2 // Reverse transformation to revert to the original number
}
Expand All @@ -4474,6 +4480,7 @@ export const transformedSchema = Schema.transform(
Schema.String, // Source schema: accepts any string
Schema.String, // Target schema: also accepts any string
{
strict: true,
decode: (s) => s.trim(), // Trim the string during decoding
encode: (s) => s // No change during encoding
}
Expand All @@ -4493,6 +4500,7 @@ export const transformedSchema = Schema.transform(
Schema.String, // Source schema: accepts any string
Schema.String.pipe(Schema.filter((s) => s === s.trim())), // Target schema now only accepts strings that are trimmed
{
strict: true,
decode: (s) => s.trim(), // Trim the string during decoding
encode: (s) => s // No change during encoding
}
Expand Down Expand Up @@ -4522,7 +4530,11 @@ const clamp =
Schema.typeSchema,
Schema.filter((a) => a <= minimum || a >= maximum)
),
{ decode: (a) => Number.clamp(a, { minimum, maximum }), encode: (a) => a }
{
strict: true,
decode: (a) => Number.clamp(a, { minimum, maximum }),
encode: (a) => a
}
)
```

Expand Down Expand Up @@ -4587,6 +4599,7 @@ export const NumberFromString = Schema.transformOrFail(
Schema.String, // Source schema: accepts any string
Schema.Number, // Target schema: expects a number
{
strict: true, // optional but you get better error messages from TypeScript
decode: (input, options, ast) => {
const parsed = parseFloat(input)
if (isNaN(parsed)) {
Expand Down Expand Up @@ -4637,6 +4650,7 @@ const PeopleId = Schema.String.pipe(Schema.brand("PeopleId"))

// Define a schema with async transformation
const PeopleIdFromString = Schema.transformOrFail(Schema.String, PeopleId, {
strict: true,
decode: (s, _, ast) =>
Effect.mapBoth(api(`https://swapi.dev/api/people/${s}`), {
onFailure: (e) => new ParseResult.Type(ast, s, e.message),
Expand Down Expand Up @@ -4705,6 +4719,7 @@ const api = (url: string): Effect.Effect<unknown, Error, "Fetch"> =>
const PeopleId = Schema.String.pipe(Schema.brand("PeopleId"))

const PeopleIdFromString = Schema.transformOrFail(Schema.String, PeopleId, {
strict: true,
decode: (s, _, ast) =>
Effect.mapBoth(api(`https://swapi.dev/api/people/${s}`), {
onFailure: (e) => new ParseResult.Type(ast, s, e.message),
Expand Down Expand Up @@ -5538,6 +5553,7 @@ const schema = Schema.transformOrFail(
Schema.String,
Schema.String.pipe(Schema.minLength(2)),
{
strict: true,
decode: (s, _, ast) =>
s.length > 0
? ParseResult.succeed(s)
Expand Down Expand Up @@ -5774,6 +5790,7 @@ const IntFromString = Schema.transformOrFail(
// This message is displayed only if the input can be converted to a number but it's not an integer
Schema.Int.annotations({ message: () => "please enter an integer" }),
{
strict: true,
decode: (s, _, ast) => {
const n = Number(s)
return Number.isNaN(n)
Expand Down Expand Up @@ -7905,6 +7922,7 @@ const NormalizeUrlString = Schema.transformOrFail(
Schema.String,
NormalizedUrlString,
{
strict: true,
decode: (value, _, ast) =>
ParseResult.try({
try: () => new URL(value).toString(),
Expand Down
Loading

0 comments on commit 8513d7e

Please sign in to comment.