-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schema: Add
standard
API to Generate a Standard Schema
- Loading branch information
Showing
5 changed files
with
362 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
"effect": minor | ||
--- | ||
|
||
Schema: Add `standardSchemaV1` API to Generate a [Standard Schema v1](https://standardschema.dev/). | ||
|
||
**Example** | ||
|
||
```ts | ||
import { Schema } from "effect" | ||
|
||
const schema = Schema.Struct({ | ||
name: Schema.String | ||
}) | ||
|
||
// ┌─── StandardSchemaV1<{ readonly name: string; }> | ||
// ▼ | ||
const standardSchema = Schema.standardSchemaV1(schema) | ||
``` |
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 |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
"zod": "^3.23.5" | ||
}, | ||
"dependencies": { | ||
"@standard-schema/spec": "^1.0.0", | ||
"fast-check": "^3.23.1" | ||
} | ||
} |
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
249 changes: 249 additions & 0 deletions
249
packages/effect/test/Schema/Schema/standardSchemaV1.test.ts
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 |
---|---|---|
@@ -0,0 +1,249 @@ | ||
import type { StandardSchemaV1 } from "@standard-schema/spec" | ||
import { Context, Effect, ParseResult, Predicate, Schema } from "effect" | ||
import { assertTrue, deepStrictEqual, strictEqual } from "effect/test/util" | ||
import { describe, it } from "vitest" | ||
import { AsyncString } from "../TestUtils.js" | ||
|
||
function validate<I, A>( | ||
schema: StandardSchemaV1<I, A>, | ||
input: unknown | ||
): StandardSchemaV1.Result<A> | Promise<StandardSchemaV1.Result<A>> { | ||
return schema["~standard"].validate(input) | ||
} | ||
|
||
const isPromise = (value: unknown): value is Promise<unknown> => value instanceof Promise | ||
|
||
const expectSuccess = async <A>( | ||
result: StandardSchemaV1.Result<A>, | ||
a: A | ||
) => { | ||
deepStrictEqual(result, { value: a }) | ||
} | ||
|
||
const expectFailure = async <A>( | ||
result: StandardSchemaV1.Result<A>, | ||
issues: ReadonlyArray<StandardSchemaV1.Issue> | ((issues: ReadonlyArray<StandardSchemaV1.Issue>) => void) | ||
) => { | ||
if (result.issues !== undefined) { | ||
if (Predicate.isFunction(issues)) { | ||
issues(result.issues) | ||
} else { | ||
deepStrictEqual(result.issues, issues) | ||
} | ||
} else { | ||
throw new Error("Expected issues, got undefined") | ||
} | ||
} | ||
|
||
const expectSyncSuccess = <I, A>( | ||
schema: StandardSchemaV1<I, A>, | ||
input: unknown, | ||
a: A | ||
) => { | ||
const result = validate(schema, input) | ||
if (isPromise(result)) { | ||
throw new Error("Expected value, got promise") | ||
} else { | ||
expectSuccess(result, a) | ||
} | ||
} | ||
|
||
const expectAsyncSuccess = async <I, A>( | ||
schema: StandardSchemaV1<I, A>, | ||
input: unknown, | ||
a: A | ||
) => { | ||
const result = validate(schema, input) | ||
if (isPromise(result)) { | ||
expectSuccess(await result, a) | ||
} else { | ||
throw new Error("Expected promise, got value") | ||
} | ||
} | ||
|
||
const expectSyncFailure = <I, A>( | ||
schema: StandardSchemaV1<I, A>, | ||
input: unknown, | ||
issues: ReadonlyArray<StandardSchemaV1.Issue> | ((issues: ReadonlyArray<StandardSchemaV1.Issue>) => void) | ||
) => { | ||
const result = validate(schema, input) | ||
if (isPromise(result)) { | ||
throw new Error("Expected value, got promise") | ||
} else { | ||
expectFailure(result, issues) | ||
} | ||
} | ||
|
||
const expectAsyncFailure = async <I, A>( | ||
schema: StandardSchemaV1<I, A>, | ||
input: unknown, | ||
issues: ReadonlyArray<StandardSchemaV1.Issue> | ((issues: ReadonlyArray<StandardSchemaV1.Issue>) => void) | ||
) => { | ||
const result = validate(schema, input) | ||
if (isPromise(result)) { | ||
expectFailure(await result, issues) | ||
} else { | ||
throw new Error("Expected promise, got value") | ||
} | ||
} | ||
|
||
const AsyncNonEmptyString = AsyncString.pipe(Schema.minLength(1)) | ||
|
||
describe("standardSchemaV1", () => { | ||
it("sync decoding + sync issue formatting", () => { | ||
const schema = Schema.NonEmptyString | ||
const standardSchema = Schema.standardSchemaV1(schema) | ||
expectSyncSuccess(standardSchema, "a", "a") | ||
expectSyncFailure(standardSchema, null, [ | ||
{ | ||
message: "Expected string, actual null", | ||
path: [] | ||
} | ||
]) | ||
expectSyncFailure(standardSchema, "", [ | ||
{ | ||
message: `Expected a non empty string, actual ""`, | ||
path: [] | ||
} | ||
]) | ||
}) | ||
|
||
it("sync decoding + sync custom message", () => { | ||
const schema = Schema.NonEmptyString.annotations({ message: () => Effect.succeed("my message") }) | ||
const standardSchema = Schema.standardSchemaV1(schema) | ||
expectSyncSuccess(standardSchema, "a", "a") | ||
expectSyncFailure(standardSchema, null, [ | ||
{ | ||
message: "Expected string, actual null", | ||
path: [] | ||
} | ||
]) | ||
expectSyncFailure(standardSchema, "", [ | ||
{ | ||
message: "my message", | ||
path: [] | ||
} | ||
]) | ||
}) | ||
|
||
it("sync decoding + async custom message", async () => { | ||
const schema = Schema.NonEmptyString.annotations({ | ||
message: () => Effect.succeed("my message").pipe(Effect.delay("10 millis")) | ||
}) | ||
const standardSchema = Schema.standardSchemaV1(schema) | ||
expectSyncSuccess(standardSchema, "a", "a") | ||
await expectAsyncFailure(standardSchema, null, [ | ||
{ | ||
message: "Expected string, actual null", | ||
path: [] | ||
} | ||
]) | ||
await expectAsyncFailure(standardSchema, "", [ | ||
{ | ||
message: "my message", | ||
path: [] | ||
} | ||
]) | ||
}) | ||
|
||
it("async decoding + sync issue formatting", async () => { | ||
const schema = AsyncNonEmptyString | ||
const standardSchema = Schema.standardSchemaV1(schema) | ||
await expectAsyncSuccess(standardSchema, "a", "a") | ||
expectSyncFailure(standardSchema, null, [ | ||
{ | ||
message: "Expected string, actual null", | ||
path: [] | ||
} | ||
]) | ||
await expectAsyncFailure(standardSchema, "", [ | ||
{ | ||
message: `Expected a string at least 1 character(s) long, actual ""`, | ||
path: [] | ||
} | ||
]) | ||
}) | ||
|
||
it("async decoding + sync custom message", async () => { | ||
const schema = AsyncNonEmptyString.annotations({ message: () => Effect.succeed("my message") }) | ||
const standardSchema = Schema.standardSchemaV1(schema) | ||
await expectAsyncSuccess(standardSchema, "a", "a") | ||
expectSyncFailure(standardSchema, null, [ | ||
{ | ||
message: "Expected string, actual null", | ||
path: [] | ||
} | ||
]) | ||
await expectAsyncFailure(standardSchema, "", [ | ||
{ | ||
message: "my message", | ||
path: [] | ||
} | ||
]) | ||
}) | ||
|
||
it("async decoding + async custom message", async () => { | ||
const schema = AsyncNonEmptyString.annotations({ | ||
message: () => Effect.succeed("my message").pipe(Effect.delay("10 millis")) | ||
}) | ||
const standardSchema = Schema.standardSchemaV1(schema) | ||
await expectAsyncSuccess(standardSchema, "a", "a") | ||
await expectAsyncFailure(standardSchema, null, [ | ||
{ | ||
message: "Expected string, actual null", | ||
path: [] | ||
} | ||
]) | ||
await expectAsyncFailure(standardSchema, "", [ | ||
{ | ||
message: "my message", | ||
path: [] | ||
} | ||
]) | ||
}) | ||
|
||
describe("missing dependencies", () => { | ||
class MagicNumber extends Context.Tag("Min")<MagicNumber, number>() {} | ||
|
||
it("sync decoding should throw", () => { | ||
const DepString = Schema.transformOrFail(Schema.Number, Schema.Number, { | ||
strict: true, | ||
decode: (n) => | ||
Effect.gen(function*(_) { | ||
const magicNumber = yield* MagicNumber | ||
return n * magicNumber | ||
}), | ||
encode: ParseResult.succeed | ||
}) | ||
|
||
const schema = DepString | ||
const standardSchema = Schema.standardSchemaV1(schema as any) | ||
expectSyncFailure(standardSchema, 1, (issues) => { | ||
strictEqual(issues.length, 1) | ||
deepStrictEqual(issues[0].path, undefined) | ||
assertTrue(issues[0].message.includes("Service not found: Min")) | ||
}) | ||
}) | ||
|
||
it("async decoding should throw", () => { | ||
const DepString = Schema.transformOrFail(Schema.Number, Schema.Number, { | ||
strict: true, | ||
decode: (n) => | ||
Effect.gen(function*(_) { | ||
const magicNumber = yield* MagicNumber | ||
yield* Effect.sleep("10 millis") | ||
return n * magicNumber | ||
}), | ||
encode: ParseResult.succeed | ||
}) | ||
|
||
const schema = DepString | ||
const standardSchema = Schema.standardSchemaV1(schema as any) | ||
expectSyncFailure(standardSchema, 1, (issues) => { | ||
strictEqual(issues.length, 1) | ||
deepStrictEqual(issues[0].path, undefined) | ||
assertTrue(issues[0].message.includes("Service not found: Min")) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.