Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Correct Handling of JSON Schema Annotations in Refinements #3284

Merged
merged 1 commit into from
Jul 17, 2024

Conversation

gcanti
Copy link
Contributor

@gcanti gcanti commented Jul 17, 2024

Fixes an issue where the JSON schema annotation set by a refinement after a transformation was mistakenly interpreted as an override annotation. This caused the output to be incorrect, as the annotations were not applied as intended.

Before

import { JSONSchema, Schema } from "@effect/schema"

const schema = Schema.Trim.pipe(Schema.nonEmpty())

const jsonSchema = JSONSchema.make(schema)
console.log(JSON.stringify(jsonSchema, null, 2))
/*
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "minLength": 1
}
*/

Now

import { JSONSchema, Schema } from "@effect/schema"

const schema = Schema.Trim.pipe(Schema.nonEmpty())

const jsonSchema = JSONSchema.make(schema)
console.log(JSON.stringify(jsonSchema, null, 2))
/*
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "string"
}
*/

@gcanti gcanti added the schema label Jul 17, 2024
Copy link

changeset-bot bot commented Jul 17, 2024

🦋 Changeset detected

Latest commit: 8f12f16

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 24 packages
Name Type
@effect/schema Patch
@effect/cli Patch
@effect/cluster-browser Patch
@effect/cluster-node Patch
@effect/cluster-workflow Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/platform Patch
@effect/rpc-http Patch
@effect/rpc Patch
@effect/sql Patch
@effect/sql-d1 Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-node Patch
@effect/sql-drizzle Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gcanti gcanti merged commit 3ac2d76 into main Jul 17, 2024
9 checks passed
@gcanti gcanti deleted the schema-3283 branch July 17, 2024 15:36
@github-actions github-actions bot mentioned this pull request Jul 17, 2024
suddenlyGiovanni added a commit to suddenlyGiovanni/resume that referenced this pull request Sep 3, 2024
- [#3287](Effect-TS/effect#3287) [`f0285d3`](Effect-TS/effect@f0285d3) Thanks @gcanti! - JSON Schema: change default behavior for property signatures containing `undefined`

  Changed the default behavior when encountering a required property signature whose type contains `undefined`. Instead of raising an exception, `undefined` is now pruned and **the field is set as optional**.

  Before

  ```ts
  import { JSONSchema, Schema } from "@effect/schema"

  const schema = Schema.Struct({
    a: Schema.NullishOr(Schema.Number)
  })

  const jsonSchema = JSONSchema.make(schema)
  console.log(JSON.stringify(jsonSchema, null, 2))
  /*
  throws
  Error: Missing annotation
  at path: ["a"]
  details: Generating a JSON Schema for this schema requires a "jsonSchema" annotation
  schema (UndefinedKeyword): undefined
  */
  ```

  Now

  ```ts
  import { JSONSchema, Schema } from "@effect/schema"

  const schema = Schema.Struct({
    a: Schema.NullishOr(Schema.Number)
  })

  const jsonSchema = JSONSchema.make(schema)
  console.log(JSON.stringify(jsonSchema, null, 2))
  /*
  {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": [], // <=== empty
    "properties": {
      "a": {
        "anyOf": [
          {
            "type": "number"
          },
          {
            "$ref": "#/$defs/null"
          }
        ]
      }
    },
    "additionalProperties": false,
    "$defs": {
      "null": {
        "const": null
      }
    }
  }
  */
  ```

- [#3291](Effect-TS/effect#3291) [`8ec4955`](Effect-TS/effect@8ec4955) Thanks @gcanti! - remove type-level error message from `optional` signature, closes #3290

  This fix eliminates the type-level error message from the `optional` function signature, which was causing issues in generic contexts.

- [#3284](Effect-TS/effect#3284) [`3ac2d76`](Effect-TS/effect@3ac2d76) Thanks @gcanti! - Fix: Correct Handling of JSON Schema Annotations in Refinements

  Fixes an issue where the JSON schema annotation set by a refinement after a transformation was mistakenly interpreted as an override annotation. This caused the output to be incorrect, as the annotations were not applied as intended.

  Before

  ```ts
  import { JSONSchema, Schema } from "@effect/schema"

  const schema = Schema.Trim.pipe(Schema.nonEmpty())

  const jsonSchema = JSONSchema.make(schema)
  console.log(JSON.stringify(jsonSchema, null, 2))
  /*
  {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "minLength": 1
  }
  */
  ```

  Now

  ```ts
  import { JSONSchema, Schema } from "@effect/schema"

  const schema = Schema.Trim.pipe(Schema.nonEmpty())

  const jsonSchema = JSONSchema.make(schema)
  console.log(JSON.stringify(jsonSchema, null, 2))
  /*
  {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "string"
  }
  */
  ```

- Updated dependencies [[`cc327a1`](Effect-TS/effect@cc327a1), [`4bfe4fb`](Effect-TS/effect@4bfe4fb), [`2b14d18`](Effect-TS/effect@2b14d18)]:
  - [email protected]

Signed-off-by: Giovanni Ravalico <[email protected]>
suddenlyGiovanni added a commit to suddenlyGiovanni/resume that referenced this pull request Sep 3, 2024
- [#3287](Effect-TS/effect#3287) [`f0285d3`](Effect-TS/effect@f0285d3) - JSON Schema: change default behavior for property signatures containing `undefined`

  Changed the default behavior when encountering a required property signature whose type contains `undefined`. Instead of raising an exception, `undefined` is now pruned and **the field is set as optional**.

  Before

  ```ts
  import { JSONSchema, Schema } from "@effect/schema"

  const schema = Schema.Struct({
    a: Schema.NullishOr(Schema.Number)
  })

  const jsonSchema = JSONSchema.make(schema)
  console.log(JSON.stringify(jsonSchema, null, 2))
  /*
  throws
  Error: Missing annotation
  at path: ["a"]
  details: Generating a JSON Schema for this schema requires a "jsonSchema" annotation
  schema (UndefinedKeyword): undefined
  */
  ```

  Now

  ```ts
  import { JSONSchema, Schema } from "@effect/schema"

  const schema = Schema.Struct({
    a: Schema.NullishOr(Schema.Number)
  })

  const jsonSchema = JSONSchema.make(schema)
  console.log(JSON.stringify(jsonSchema, null, 2))
  /*
  {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": [], // <=== empty
    "properties": {
      "a": {
        "anyOf": [
          {
            "type": "number"
          },
          {
            "$ref": "#/$defs/null"
          }
        ]
      }
    },
    "additionalProperties": false,
    "$defs": {
      "null": {
        "const": null
      }
    }
  }
  */
  ```

- [#3291](Effect-TS/effect#3291) [`8ec4955`](Effect-TS/effect@8ec4955) - remove type-level error message from `optional` signature, closes #3290

  This fix eliminates the type-level error message from the `optional` function signature, which was causing issues in generic contexts.

- [#3284](Effect-TS/effect#3284) [`3ac2d76`](Effect-TS/effect@3ac2d76) - Fix: Correct Handling of JSON Schema Annotations in Refinements

  Fixes an issue where the JSON schema annotation set by a refinement after a transformation was mistakenly interpreted as an override annotation. This caused the output to be incorrect, as the annotations were not applied as intended.

  Before

  ```ts
  import { JSONSchema, Schema } from "@effect/schema"

  const schema = Schema.Trim.pipe(Schema.nonEmpty())

  const jsonSchema = JSONSchema.make(schema)
  console.log(JSON.stringify(jsonSchema, null, 2))
  /*
  {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "minLength": 1
  }
  */
  ```

  Now

  ```ts
  import { JSONSchema, Schema } from "@effect/schema"

  const schema = Schema.Trim.pipe(Schema.nonEmpty())

  const jsonSchema = JSONSchema.make(schema)
  console.log(JSON.stringify(jsonSchema, null, 2))
  /*
  {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "string"
  }
  */
  ```

- Updated dependencies [[`cc327a1`](Effect-TS/effect@cc327a1), [`4bfe4fb`](Effect-TS/effect@4bfe4fb), [`2b14d18`](Effect-TS/effect@2b14d18)]:
  - [email protected]

Signed-off-by: Giovanni Ravalico <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

1 participant