ciscoheat
ciscoheat
Explore posts from servers
Aarktype
Created by ciscoheat on 4/28/2025 in #questions
Add property to object based on its data
I have a list of records (CSV to JSON) where each row has a variable numbers of attributes in its columns. It could be something like Attribute 1 name and Attribute 1 value, and so on for 2, 3, 4... On the output object, I'd like to make a Record where the name is the key, and it points to an object based on the attribute data (name and value in this case). So if the product type is (simplified):
const product = type({
id: "string",
attributes: type({
"[string]": type({
name: "string",
value: "string"
}),
})
})
const product = type({
id: "string",
attributes: type({
"[string]": type({
name: "string",
value: "string"
}),
})
})
Then there are two problems: 1. The attributes property doesn't exist on the type, so it needs to be added. Right now I get a must be an object (was missing) error. How to add that without any type checking, so to speak? 2. Then I guess I need to pipe the product type and parse all the attributes there, so they can be added to the attributes property, is that right?
5 replies
Aarktype
Created by ciscoheat on 4/8/2025 in #questions
Empty string or number
I have this type that I want to reuse:
const number = type("string")
.pipe((s) => s || "0")
.to("string.numeric.parse");
const number = type("string")
.pipe((s) => s || "0")
.to("string.numeric.parse");
I want to reuse it so if the string is empty, leave it as is, otherwise parse it as the number type. But when I do this, it complains:
type("''").or(number); // An unordered union of a type including a morph and a type with overlapping input is indeterminate
type("''").or(number); // An unordered union of a type including a morph and a type with overlapping input is indeterminate
This works though, so it's no big deal but just curious if it's possible to combine these above?
type("''").or("string.numeric.parse");
type("''").or("string.numeric.parse");
16 replies
DDeno
Created by ciscoheat on 4/7/2025 in #help
When to use deno add compared to import with a prefix?
As the title says, I can import like this:
import { parse } from "jsr:@std/csv/parse";
import { parse } from "jsr:@std/csv/parse";
And it will work, but there is also deno add, which I'm recommended to use if I import from @std/csv/parse. When should I use deno add?
3 replies
Aarktype
Created by ciscoheat on 3/8/2025 in #questions
Get keys of object type
What's the easiest way to get the keys of an object type, and perhaps also some metadata for it? I see there's an keyof() method, but it has plenty of methods itself so I'm not sure what to use.
29 replies
DTDrizzle Team
Created by ciscoheat on 2/8/2025 in #help
Is it possible to seed nested referenced entities?
If I have three tables with a one to many relations: Agency -> Project -> Domain I see in the docs that with refine you can create references from one table to another:
await seed(db, schema).refine(f => ({
agency: {
count: 3,
with: {
project: 4
}
}
}))
await seed(db, schema).refine(f => ({
agency: {
count: 3,
with: {
project: 4
}
}
}))
But I would also like to create references to domains, however I cannot keep nesting after project. How to create relationships between project and domain, so the project refers to an agency?
2 replies
Aarktype
Created by ciscoheat on 2/2/2025 in #questions
Validate Record values with morphs
Is it possible to do more advanced validation on Records? Ideally I'd like to apply any morph to each item in a Record.
const nullNumber = type('number').pipe((s) => s ? Number(s) : null);

const obj = type({
searches: 'Record<string, ???>'
});
const nullNumber = type('number').pipe((s) => s ? Number(s) : null);

const obj = type({
searches: 'Record<string, ???>'
});
In the ??? place, I'd like to validate against nullNumber, is it possible?
9 replies
Aarktype
Created by ciscoheat on 2/1/2025 in #questions
Testing a type in pipe
Congratulations on the 2.0 release, it's working great! 😀 Is it possible/best practice to run a type inside pipe? I'm having this test condition:
If string is a valid url, return URL
Else return null
If string is a valid url, return URL
Else return null
I'm not sure of the best way to handle this scenario with morphs? I see there's a second parameter for pipe.try, can it be used to simplify?
8 replies
Aarktype
Created by ciscoheat on 5/11/2024 in #questions
Problem with validation in 2.0 dev?
I'm using 2.0.0-dev.11 and cannot figure out why some very basic validation fails. Short example:
import { type } from "arktype";

const nospacePattern = /^\S*$/;

const schema = type({
name: "string",
email: "email",
tags: "(string>=2)[]>=3",
score: "integer>=0",
"date?": "Date",
"nospace?": nospacePattern,
extra: "string|null",
});

const data = {
name: "Ok",
email: "",
tags: ["AB", "B"],
score: -1,
date: undefined,
nospace: "One space",
};

const out = schema(data);

console.log("nospace matches:", nospacePattern.test(data.nospace));

if (out instanceof type.errors) {
console.log(out.summary);
} else {
console.log(out);
}
import { type } from "arktype";

const nospacePattern = /^\S*$/;

const schema = type({
name: "string",
email: "email",
tags: "(string>=2)[]>=3",
score: "integer>=0",
"date?": "Date",
"nospace?": nospacePattern,
extra: "string|null",
});

const data = {
name: "Ok",
email: "",
tags: ["AB", "B"],
score: -1,
date: undefined,
nospace: "One space",
};

const out = schema(data);

console.log("nospace matches:", nospacePattern.test(data.nospace));

if (out instanceof type.errors) {
console.log(out.summary);
} else {
console.log(out);
}
The output I get from running this is:
nospace matches: false
email must be a valid email (was "")
extra must be a string or null (was missing)
date must be a Date (was undefined)
nospace matches: false
email must be a valid email (was "")
extra must be a string or null (was missing)
date must be a Date (was undefined)
I'd expected errors for score, nospace, tags and maybe tags[1] as well. Anything very simple I'm missing?
16 replies
DTDrizzle Team
Created by ciscoheat on 4/16/2024 in #help
Using parameter in LIKE
Is it safe to pass a string directly into a like condition, or does it have to be quoted or parameterized somehow?
where(like(entries.text, `%${filters.search}%`))
where(like(entries.text, `%${filters.search}%`))
3 replies
Aarktype
Created by ciscoheat on 12/23/2023 in #questions
Array error message
Hello, I think I found an inconsistency in array errors: https://stackblitz.com/edit/vy55da?file=type.ts The tags error is: tags must be at least 3 characters (was 2) But should probably not be characters, but something like items.
3 replies