Simon
Simon
Explore posts from servers
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Oh I missed that, makes sense
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
I'm having some weird results with the select but maybe I'm misunderstanding how it works. Given the following schema:
const Thing = type({
uid: type("string"),
"summary": type("string <= 4000 | undefined").optional(),
"description?": type("string <= 4000 | undefined"),
})
const Thing = type({
uid: type("string"),
"summary": type("string <= 4000 | undefined").optional(),
"description?": type("string <= 4000 | undefined"),
})
const out = Thing.select({ kind: "optional" }).map(c => c.toString())
const out = Thing.select({ kind: "optional" }).map(c => c.toString())
Prints:
[
"Type<description?: string <= 4000 | undefined>",
"Type<summary?: string <= 4000 | undefined>"
]
[
"Type<description?: string <= 4000 | undefined>",
"Type<summary?: string <= 4000 | undefined>"
]
Which is expected.
const out = Thing.get('summary').select({ kind: "optional" }).map(c => c.toString())
const out = Thing.get('summary').select({ kind: "optional" }).map(c => c.toString())
This however returns an empty array.
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Looks like my isOptional is not stable for all schemas. Is something like this supposed to be the way?
export function isOptional(fieldSchema: Type) {
const optionalConstraints = fieldSchema.select({
boundary: "shallow",
kind: "unit",
});

return (
optionalConstraints.filter((constraint) => constraint.optional).length > 0
);
}
export function isOptional(fieldSchema: Type) {
const optionalConstraints = fieldSchema.select({
boundary: "shallow",
kind: "unit",
});

return (
optionalConstraints.filter((constraint) => constraint.optional).length > 0
);
}
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
@ArkDavid is the .select function costly? I wonder if I can run a few of those for each field and automatically add the components (like "— optional" or "0 / 4000 characters") if the constraints exists or if there will be a heavy price for large forms
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Makes sens yeah
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Just tested and that's awesome. I could quickly create a few helpers that are easily typed & reused
import type { Type } from "arktype";

/**
* Check if the field is optional
*/
export function isOptional(fieldSchema: Type) {
const requiredConstraints = fieldSchema.select({
kind: "required",
});

return requiredConstraints.length === 0;
}

/**
* Retrieve the maxLength rule from the schema
*/
export function getMaxLength(fieldSchema: Type) {
const maxLengthConstraints = fieldSchema.select({
kind: "maxLength",
boundary: "shallow",
});

if (maxLengthConstraints.length === 0) {
return null;
}

return maxLengthConstraints.reduce((max, constraint) => {
if (constraint.rule > max) {
return constraint.rule;
}

return max;
}, 0);
}
import type { Type } from "arktype";

/**
* Check if the field is optional
*/
export function isOptional(fieldSchema: Type) {
const requiredConstraints = fieldSchema.select({
kind: "required",
});

return requiredConstraints.length === 0;
}

/**
* Retrieve the maxLength rule from the schema
*/
export function getMaxLength(fieldSchema: Type) {
const maxLengthConstraints = fieldSchema.select({
kind: "maxLength",
boundary: "shallow",
});

if (maxLengthConstraints.length === 0) {
return null;
}

return maxLengthConstraints.reduce((max, constraint) => {
if (constraint.rule > max) {
return constraint.rule;
}

return max;
}, 0);
}
const fieldSchema = schema.get(field.name);
const maxLength = getMaxLength(fieldSchema);
const fieldSchema = schema.get(field.name);
const maxLength = getMaxLength(fieldSchema);
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Very interesting. That’d make it really more easy to deal with!
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Thanks for guiding me through this 🙂 I think I have what I need to go further now
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Ah yeah, also need to check if not null
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
bummer that I can't seem to narrow to the JsonObject instead of dismissing string, boolean, number etc..
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Got to something with some checks.
const summaryProperty = schema.get("summary").json;
const properties = Array.isArray(summaryProperty)
? summaryProperty
: [summaryProperty];
const max = properties.reduce((max, property) => {
if (property === null) return max;
if (typeof property === "string") return max;
if (typeof property === "number") return max;
if (typeof property === "boolean") return max;
if (Array.isArray(property)) return max;

console.log("property", property, Object.keys(property));
if (Object.keys(property).includes("maxLength") && property.maxLength) {
console.log("maxLength", property.maxLength);
const maxLength = property.maxLength;

if (maxLength === null) return max;
if (typeof maxLength === "string") return max;
if (typeof maxLength === "number") return max;
if (typeof maxLength === "boolean") return max;
if (Array.isArray(maxLength)) return max;

return maxLength.rule;
} else {
console.log("not maxLength", property.maxLength);
}

return max;
}, 0);
const summaryProperty = schema.get("summary").json;
const properties = Array.isArray(summaryProperty)
? summaryProperty
: [summaryProperty];
const max = properties.reduce((max, property) => {
if (property === null) return max;
if (typeof property === "string") return max;
if (typeof property === "number") return max;
if (typeof property === "boolean") return max;
if (Array.isArray(property)) return max;

console.log("property", property, Object.keys(property));
if (Object.keys(property).includes("maxLength") && property.maxLength) {
console.log("maxLength", property.maxLength);
const maxLength = property.maxLength;

if (maxLength === null) return max;
if (typeof maxLength === "string") return max;
if (typeof maxLength === "number") return max;
if (typeof maxLength === "boolean") return max;
if (Array.isArray(maxLength)) return max;

return maxLength.rule;
} else {
console.log("not maxLength", property.maxLength);
}

return max;
}, 0);
I suppose I can make helpers and use those as I won't have that many different cases
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
// title property
{
"domain": {
"domain": "string",
"meta": {
"message": "$ark.message1"
}
},
"minLength": {
"rule": 3,
"meta": {
"message": "$ark.message1"
}
},
"meta": {
"message": "$ark.message1"
}
}
// title property
{
"domain": {
"domain": "string",
"meta": {
"message": "$ark.message1"
}
},
"minLength": {
"rule": 3,
"meta": {
"message": "$ark.message1"
}
},
"meta": {
"message": "$ark.message1"
}
}
// summary property
[
{
"domain": {
"domain": "string",
"meta": {
"message": "$ark.message2"
}
},
"maxLength": {
"rule": 4000,
"meta": {
"message": "$ark.message2"
}
},
"meta": {
"message": "$ark.message2"
}
},
{
"unit": "undefined"
}
]
// summary property
[
{
"domain": {
"domain": "string",
"meta": {
"message": "$ark.message2"
}
},
"maxLength": {
"rule": 4000,
"meta": {
"message": "$ark.message2"
}
},
"meta": {
"message": "$ark.message2"
}
},
{
"unit": "undefined"
}
]
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
depending on the number of rules I suppose or the kind of rules
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Looks like it returns either an object or an array of objects
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Ah yes, that looks promising
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Indeed, is there a way to get the "maxLength" from that?
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
Not sure how I could use the type itself 🤔 I don't want to validate, I want to get the validation rules if that matters
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
here's my schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"title": {
"type": "string",
"message": "$ark.message1",
"minLength": 3
},
"uid": {
"type": "string",
"message": "$ark.message"
},
"summary": {
"type": "string",
"message": "$ark.message2",
"maxLength": 3999
}
},
"required": [
"title",
"uid"
]
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"title": {
"type": "string",
"message": "$ark.message1",
"minLength": 3
},
"uid": {
"type": "string",
"message": "$ark.message"
},
"summary": {
"type": "string",
"message": "$ark.message2",
"maxLength": 3999
}
},
"required": [
"title",
"uid"
]
}
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
It feels kind of counterproductive. My goal is to reuse the schema to get the values like required/maxLength. If I check property access it feels like a rabbit hole. My first tests were unconclusive. Here's a sample:
if (schema.type === "object" && schema.properties?.[field.name] && schema.properties[field.name]?.type === "string") {
max = schema.properties[field.name].maxLength
}
if (schema.type === "object" && schema.properties?.[field.name] && schema.properties[field.name]?.type === "string") {
max = schema.properties[field.name].maxLength
}
In this instance, type does not exist in schema.properties[field.name]
Property 'type' does not exist on type 'NonBooleanBranch'. Property 'type' does not exist on type 'Const'.ts(2339)
53 replies
Aarktype
Created by Simon on 4/23/2025 in #questions
Can the JSON Schema be typed automatically?
That's a bummer. Is there any know libs that do the heavy lifting for us?
53 replies