-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
Allow string format as metadata associated with JSON schema #1110
Comments
I had a small look into the source and you could probably just quickly patch it by adding parsableDate.toJsonSchema = () => ({
"anyOf": [
{
"type": "string",
"format": "date-time",
},
{
"type": "string",
"format": "date",
}
]
}) into I'm not sure if this is where you want to do that, further you probably want some tests for that. |
I'll have to think about this a bit since If you're okay with a specific date format like ISO8601, you can use something like const user = type({
name: "string",
birthday: "string.date.iso.parse"
})
const schema = user.in.toJsonSchema()
const result = {
type: "object",
properties: {
birthday: {
type: "string",
pattern:
"^([+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))([T]((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24:?00)([.,]\\d+(?!:))?)?(\\17[0-5]\\d([.,]\\d+)?)?([zZ]|([+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?$"
},
name: { type: "string" }
},
required: ["birthday", "name"]
} I think having a way to add "format" as metadata to a string type though would be useful, so I'm going tweak this issue a bit to reflect the broader goal. |
In theory format date or date-time means it complies with the RFC 3339 specification. I'm not sure if the current |
Yeah I can likely create some new keywords to specifically align with these standards, but generally these subtypes are stricter the further you chain them. Once we have the capability to associate JSON schema format as metadata, we'll have to ensure all the other format keywords are integrated with the type system as well. |
Issue
When using the
string.date.parse
type in combination withtoJsonSchema
I expected the type to be parsed to string, maybe even with the format keyword (see json-schema).Example
This errors with
Uncaught ParseError: Predicate $ark.isParsableDate is not convertible to JSON Schema
.Expected output:
Solution (proposed by @ssalbdivad)
Add
format
as a metadata key. This would have no effect on validation but would allow custom types likestring.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.The text was updated successfully, but these errors were encountered: