-
-
Notifications
You must be signed in to change notification settings - Fork 710
Expand file tree
/
Copy path10_schema-basics.ts
More file actions
43 lines (37 loc) · 1.59 KB
/
Copy path10_schema-basics.ts
File metadata and controls
43 lines (37 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @title Schema basics
*
* Define `Schema.Class`s, decode unknown input into typed values, and
* encode typed values back into their external representation.
*/
import { Effect, Schema } from "effect"
// Schema.Class defines both a runtime validator and a TypeScript class.
// This is useful for domain models that should only be constructed from valid
// data.
//
// The static `Type` and `Encoded` members are available when you need
// the decoded or encoded TypeScript representation.
export class User extends Schema.Class<User>("path/to/module/User")({
id: Schema.Int,
name: Schema.NonEmptyString,
email: Schema.String,
role: Schema.Literals(["admin", "member"])
}) {}
// `UserType` will be the type `User`, as schema classes use the class type as
// the validated type.
export type UserType = typeof User["Type"]
// Access the encoded type with `typeof YourSchema["Encoded"]`.
export type UserEncoded = typeof User["Encoded"]
// Reuse parsers at the edges of your application instead of rebuilding them for
// every request. Use the Effect-returning APIs when you are already inside
// Effect code so validation errors remain typed in the error channel.
export const decodeUser = Schema.decodeUnknownEffect(User)
export const encodeUser = Schema.encodeEffect(User)
export class InvalidUserPayload extends Schema.TaggedError<InvalidUserPayload>()("InvalidUserPayload", {
message: Schema.String
}) {}
export const parseUserPayload = Effect.fn("parseUserPayload")((input: unknown) =>
decodeUser(input).pipe(
Effect.mapError((error) => new InvalidUserPayload({ message: error.message }))
)
)