ToJsonSchema fails with narrow

Hi short question, i might be missing something obvious. If I try to convert a type to jsonschema with a narrow function it fails every time, even if I use the .in property.
const url = type("string")
.narrow((s, ctx) => {
if (isValidHttpUrl(s)) {
return true;
}

return ctx.mustBe("a valid URL");
});

// FAILS:
url.in.toJsonSchema()

// EXPECTED:
{ type: 'string' }
const url = type("string")
.narrow((s, ctx) => {
if (isValidHttpUrl(s)) {
return true;
}

return ctx.mustBe("a valid URL");
});

// FAILS:
url.in.toJsonSchema()

// EXPECTED:
{ type: 'string' }
21 Replies
ssalbdivad
ssalbdivad2w ago
What does the error message say?
Möhre
Möhre2w ago
Uncaught ParseError: Predicate $ark.fn19 is not convertible to JSON Schema
ssalbdivad
ssalbdivad2w ago
Well it makes sense right? It's saying you have a condition on your type that can't be represented in JSON schema so it fails
Möhre
Möhre2w ago
I guess but i thought the in property takes the input types which would be string
ssalbdivad
ssalbdivad2w ago
Maybe at some point there will be a "loose" toJsonSchema where if there are custom predicates (you narrow your type) it will strip them out, but that doesn't exist yet. Anything you can use a regex for can be converted directly It takes the input type of your transformation, but narrowing is not a transformation it's just an assertion
Möhre
Möhre2w ago
I know atleast for the url there is a type, was just an example. Would pipe work for that?
ssalbdivad
ssalbdivad2w ago
Yeah if you pipe it to itself but do the extra check there, then you can extract .in, it would work
Möhre
Möhre2w ago
I see, thanks for the clarification. A kinda loose or lax mode for that would be very helpful!
ssalbdivad
ssalbdivad2w ago
Feel free to create an issue! Would definitely be interested in adding that at some point or accepting contributions
Möhre
Möhre2w ago
Allright!
Randall
Randall6d ago
So narrow can make a type break jsonschema. Morph OK? Sometimes names help clarity. Restrict, narrow, transform, morph, validate,... thesaurus. X like y& lax semantic Any AT constructs that can feed jsonschema native constraints? OK to rtfm me, night here rn
ssalbdivad
ssalbdivad6d ago
I don't know if this was intentionally ironic but this is so hard to understand 😅
Randall
Randall6d ago
Phone kb sry
ssalbdivad
ssalbdivad6d ago
You don't just have to rely on naming just think "can this thing be represented in JSON schema"? If it's a custom function or symbol or something it can't, there are clear errors
Randall
Randall3d ago
'Course Will rety later What I was trying to say was that a) I understand using narrow() screws up the chance to represent a type as JSON-Schema. b) I'm assuming morph works fine, not affecting the JSON-Schema output. c) I wonder if some variant fitting between those two somehow could express a type that produces a valid JSON-Schema, while still capturing the functionality restricting actual data in ways that would raise (post-schema) validation failures. Just as morph could explode if it sees something it doesn't like, and recognizing that not every possible constraint/restriction/transform/validate may be expressible within JSON-Schema's capabilities, yet still be fundamentally part of the data's domain model. d) those ...names could be ideas for how to name an API call expressing such facts about the domain model. and finally e) I'm assuming that basic min/max constraints "< 4" would be representable in JSON-Schema (are they?) ... and others?
Want results from more Discord servers?
Add your server