Skip to content

Commit 0a3e3e1

Browse files
authored
Schema: standardSchemaV1 now includes the schema, closes #4494 (#4648)
1 parent f87991b commit 0a3e3e1

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

.changeset/shy-pots-dream.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Schema: `standardSchemaV1` now includes the schema, closes #4494.
6+
7+
This update fixes an issue where passing `Schema.standardSchemaV1(...)` directly to `JSONSchema.make` would throw a `TypeError`. The schema was missing from the returned object, causing the JSON schema generation to fail.
8+
9+
Now `standardSchemaV1` includes the schema itself, so it can be used with `JSONSchema.make` without issues.
10+
11+
**Example**
12+
13+
```ts
14+
import { JSONSchema, Schema } from "effect"
15+
16+
const Person = Schema.Struct({
17+
name: Schema.optionalWith(Schema.NonEmptyString, { exact: true })
18+
})
19+
20+
const standardSchema = Schema.standardSchemaV1(Person)
21+
22+
console.log(JSONSchema.make(standardSchema))
23+
/*
24+
{
25+
'$schema': 'http://json-schema.org/draft-07/schema#',
26+
'$defs': {
27+
NonEmptyString: {
28+
type: 'string',
29+
description: 'a non empty string',
30+
title: 'nonEmptyString',
31+
minLength: 1
32+
}
33+
},
34+
type: 'object',
35+
required: [],
36+
properties: { name: { '$ref': '#/$defs/NonEmptyString' } },
37+
additionalProperties: false
38+
}
39+
*/
40+
```

packages/effect/dtslint/Schema/Schema.tst.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { StandardSchemaV1 } from "@standard-schema/spec"
12
import type {
23
Arbitrary,
34
BigDecimal,
@@ -4247,4 +4248,10 @@ describe("Schema", () => {
42474248
expect(schema.to).type.toBe<typeof S.Number>()
42484249
})
42494250
})
4251+
4252+
it("standardSchemaV1", () => {
4253+
const standardSchema = S.standardSchemaV1(S.NumberFromString)
4254+
expect(S.asSchema(standardSchema)).type.toBe<S.Schema<number, string>>()
4255+
expect(standardSchema).type.toBe<StandardSchemaV1<string, number> & S.SchemaClass<number, string, never>>()
4256+
})
42504257
})

packages/effect/src/Schema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ const makeStandardFailureFromParseIssue = (
195195
export const standardSchemaV1 = <A, I>(
196196
schema: Schema<A, I, never>,
197197
overrideOptions?: AST.ParseOptions
198-
): StandardSchemaV1<I, A> => {
198+
): StandardSchemaV1<I, A> & SchemaClass<A, I, never> => {
199199
const decodeUnknown = ParseResult.decodeUnknown(schema, { errors: "all" })
200-
return {
201-
"~standard": {
200+
return class StandardSchemaV1Class extends make<A, I, never>(schema.ast) {
201+
static "~standard" = {
202202
version: 1,
203203
vendor: "effect",
204204
validate(value) {

packages/effect/test/Schema/Schema/standardSchemaV1.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ const expectAsyncFailure = async <I, A>(
9090
const AsyncNonEmptyString = AsyncString.pipe(Schema.minLength(1))
9191

9292
describe("standardSchemaV1", () => {
93+
it("should return a schema", () => {
94+
const schema = Schema.NumberFromString
95+
const standardSchema = Schema.standardSchemaV1(schema)
96+
assertTrue(Schema.isSchema(standardSchema))
97+
})
98+
9399
it("sync decoding + sync issue formatting", () => {
94100
const schema = Schema.NonEmptyString
95101
const standardSchema = Schema.standardSchemaV1(schema)

0 commit comments

Comments
 (0)