arktype icon indicating copy to clipboard operation
arktype copied to clipboard

Allow string format as metadata associated with JSON schema

Open semohr opened this issue 5 months ago • 4 comments

Issue

When using the string.date.parse type in combination with toJsonSchema I expected the type to be parsed to string, maybe even with the format keyword (see json-schema).

Example

import {type} from "arktype";

const date = type("string.parse.date");
date.in.toJsonSchema(); // <-- Errors

This errors with Uncaught ParseError: Predicate $ark.isParsableDate is not convertible to JSON Schema.

Expected output:

{
"type":string",
 "format": "date"
}

Solution (proposed by @ssalbdivad)

Add format as a metadata key. This would have no effect on validation but would allow custom types like string.date that rely on non-serializable predicates in the type system to be converted to JSON schema.

The format key should be added to the output JSON schema alongside any other constraints. It should be specifically added to the non-serializable conditions it should replace, in this case a predicate for validating whether a string can be parsed as a Date. If multiple format constraints exist on the same IntersectionNode, they must be identical.

semohr avatar Aug 30 '24 14:08 semohr