-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Special case
S.parseJson
to generate JSON Schemas by targeting the … (
- Loading branch information
Showing
6 changed files
with
140 additions
and
24 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,59 @@ | ||
--- | ||
"@effect/schema": patch | ||
--- | ||
|
||
Special case `S.parseJson` to generate JSON Schemas by targeting the "to" side of transformations, closes #3086 | ||
|
||
Resolved an issue where `JSONSchema.make` improperly generated JSON Schemas for schemas defined with `S.parseJson(<real schema>)`. Previously, invoking `JSONSchema.make` on these transformed schemas produced a JSON Schema corresponding to a string type rather than the underlying real schema. | ||
|
||
Before | ||
|
||
```ts | ||
import { JSONSchema, Schema } from "@effect/schema" | ||
|
||
// Define a schema that parses a JSON string into a structured object | ||
const schema = Schema.parseJson( | ||
Schema.Struct({ | ||
a: Schema.parseJson(Schema.NumberFromString) // Nested parsing from JSON string to number | ||
}) | ||
) | ||
|
||
console.log(JSONSchema.make(schema)) | ||
/* | ||
{ | ||
'$schema': 'http://json-schema.org/draft-07/schema#', | ||
'$ref': '#/$defs/JsonString', | ||
'$defs': { | ||
JsonString: { | ||
type: 'string', | ||
description: 'a JSON string', | ||
title: 'JsonString' | ||
} | ||
} | ||
} | ||
*/ | ||
``` | ||
|
||
Now | ||
|
||
```ts | ||
import { JSONSchema, Schema } from "@effect/schema" | ||
|
||
// Define a schema that parses a JSON string into a structured object | ||
const schema = Schema.parseJson( | ||
Schema.Struct({ | ||
a: Schema.parseJson(Schema.NumberFromString) // Nested parsing from JSON string to number | ||
}) | ||
) | ||
|
||
console.log(JSONSchema.make(schema)) | ||
/* | ||
{ | ||
'$schema': 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
required: [ 'a' ], | ||
properties: { a: { type: 'string', description: 'a string', title: 'string' } }, | ||
additionalProperties: false | ||
} | ||
*/ | ||
``` |
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
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
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
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
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