Effect CommunityEC
Effect Communityβ€’2y agoβ€’
11 replies
addamsson

Handling Unexpressible Refinements in JSON Schema Conversion

What should I do if I'm converting my shema to a JSON Schema and I'm using a refinement that can't be expressed with JSON Schema?
This schema is fine:
import * as JSONSchema from "@effect/schema/JSONSchema";
import * as S from "@effect/schema/Schema";
import { describe, test } from "vitest";

describe("Given a user schema", () => {
    test("When converting to json schema Then it should produce the proper output", () => {
        JSONSchema.make(
            S.struct({
                name: S.string.pipe(
                    S.identifier("Name"),
                    S.minLength(1, {
                        message: () => `Name can't be empty.`,
                        jsonSchema: { minLength: 1 },
                    }),
                ),
            }),
        );
    });
});

but if I add something that can't be represented with JSON Schema:
JSONSchema.make(
    S.struct({
        name: S.string.pipe(
            S.identifier("Name"),
            S.minLength(1, {
                message: () => `Name can't be empty.`,
                jsonSchema: { minLength: 1 },
            }),
            S.trimmed()
        ),
    }),
);

then I get:
cannot build a JSON Schema for a refinement without a JSON Schema annotation
Was this page helpful?