Skip to content

jsonSchemaToType is not allowing $ref #1485

Closed

Description

@sukeshpabolu

Report a bug

Getting Uncaught (in promise) ParseError: Provided JSON Schema must have at least one of the keys 'type', 'enum', 'const', 'allOf', 'anyOf', 'oneOf' and 'not' exception while trying to use $ref.

🔎 Search Terms

$ref, jsonSchemaToType, parseJsonSchema

🧩 Context

  • ArkType version: 2.1.20
  • TypeScript version (5.1+): 5.8.3
  • Other context you think may be relevant (JS flavor, OS, etc.): deno, windows 10

🧑‍💻 Repro

Playground Link: https://arktype.io/playground

import { scope, type } from "npm:arktype"
import { jsonSchemaToType } from "npm:@ark/json-schema"

const types = scope({ a: { b: "b" }, b: { a: "a" } }).export();

const jsonSchema = types.a.toJsonSchema();

T = jsonSchemaToType(jsonSchema)

Activity

  1. changed the title [-]not allowing $ref[/-] [+]jsonSchemaToType not allowing $ref[/+] on Aug 4, 2025
  2. changed the title [-]jsonSchemaToType not allowing $ref[/-] [+]jsonSchemaToType is not allowing $ref[/+] on Aug 4, 2025
  3. TizzySaurus commented on Aug 4, 2025

    @TizzySaurus
    Contributor

    As the person who implemented @ark/json-schema, lack of $ref support was deliberate, in the interest of getting something of value out as soon as possible.

    I think the bug here is primarily that it's not listed as a limitation in the README, like dependencies and if/else/then support.

    It's an interesting side-effect that this breaks ArkType scopes like the one you provided here. Maybe a nicer error could be provided instead (e.g. "conversion of scopes to json schema is not currently supported") in the interim?

    Will leave @ssalbdivad to formally classify this, but fwiw my thoughts are that at its core this is ultimately a feature requests for adding $ref support to @ark/json-schema.

  4. SukeshP1995 commented on Aug 5, 2025

    @SukeshP1995

    I can understand the lack of support for dependencies or if, else. But why there is no support for $ref? Could you suggest any alternative?

  5. TizzySaurus commented on Aug 5, 2025

    @TizzySaurus
    Contributor

    @SukeshP1995 if you're writing the actual JSON Schema yourself, then you can do something like:

    const repeatedSchema = {
        type: "string",
        minLength: 1,
        maxLength: 10
    } as const;
    
    const t = jsonSchemaToType({
        type: "object",
        properties: {
            name: repeatedSchema,
            surname: repeatedSchema,
            age: { type: "number" },
        }
    });
  6. sukeshpabolu commented on Aug 5, 2025

    @sukeshpabolu
    Author

    I already have json-schema in a file. I need 1 clarification. Can we use jsonSchemaToType like how we use ajv? I just need validation and get the errors.

  7. TizzySaurus commented on Aug 5, 2025

    @TizzySaurus
    Contributor

    @sukeshpabolu Yes, jsonSchemaToType can be used similarly to ajv.

    Once you have your arktype type (in my example above, t), you can do either:

    import { ArkErrors } from "arktype";
    ...
    
    const validationResult = t(someData);
    if (validationResult instanceof ArkErrors) {
        console.log(validationResult.summary); // log validation errors
        return
    }

    or:

    const validationResult = t.assert(someData); // automatically throw if validation fails
  8. sukeshpabolu commented on Aug 5, 2025

    @sukeshpabolu
    Author

    This is cool. I just need a way to handle those refs. I may have to use something like this

  9. ssalbdivad commented on Nov 18, 2026

    @ssalbdivad
    Member

    Fixed in 3.0.0-beta.0 (#1728). Try it with npm i arktype@next.

  10. moved this from To do to Done (merged or closed) in arktypeioon Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

  • Status
    Done (merged or closed)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions