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

feat(jex): extension check returns a reason on failure #120

Merged
merged 5 commits into from
Feb 15, 2024

Conversation

franklevasseur
Copy link
Member

@franklevasseur franklevasseur commented Feb 14, 2024

Example 1:

undefined ⊆ number ?
 - #: undefined ⊈ number

{a: string | undefined} ⊆ {a: string} ?
 - #.a: undefined ⊈ string

string | number | undefined | null ⊆ string | number ?
 - #: undefined ⊈ string
 - #: undefined ⊈ number
 - #: null ⊈ string
 - #: null ⊈ number

string ⊆ "banana" ?
 - #: string ⊈ "banana"

"banana" ⊆ number | "apple" ?
 - #: "banana" ⊈ number
 - #: "banana" ⊈ "apple"

string[] ⊆ number[] ?
 - #[number]: string ⊈ number

[string, number] ⊆ number[] ?
 - #[0]: string ⊈ number

Record<string, string | undefined | boolean> ⊆ Record<string, string> ?
 - #[string]: undefined ⊈ string
 - #[string]: boolean ⊈ string

Example 2:

import zodToJsonSchema from 'zod-to-json-schema'
import * as z from 'zod'
import * as jex from './src'
import { JSONSchema7 } from 'json-schema'

const z2j = (...args: Parameters<typeof zodToJsonSchema>) => zodToJsonSchema(...args) as JSONSchema7

const typeA = z2j(
  z.object({
    a: z.string(),
    b: z.tuple([
      z.any(),
      z.object({
        c: z.record(z.union([z.tuple([z.string(), z.number()]), z.object({ d: z.string() }), z.literal('e')]))
      })
    ])
  })
)

const typeB = z2j(
  z.object({
    a: z.string(),
    b: z.tuple([
      z.any(),
      z.object({
        c: z.record(z.union([z.tuple([z.string(), z.number()]), z.object({ d: z.string() })]))
      })
    ])
  })
)

jex.jsonSchemaExtends(typeA, typeB).then((extension) => console.log(extension))

// logs:
{
  extends: false,
  reasons: [
    '#.b[1].c[string]: "e" ⊈ [string, number]',
    '#.b[1].c[string]: "e" ⊈ { d: string }'
  ]
}

@franklevasseur franklevasseur merged commit 25cd8c3 into master Feb 15, 2024
1 check passed
@michaelmass michaelmass deleted the fl_jex_message_2 branch February 28, 2024 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants