aabad_ankit
aabad_ankit
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
Thank you so much for all the help
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
@ArkDavid You were suggesting to use narrow, can I do it on union and solve my problem
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
a code sample will be super useful
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
but delete on undeclared key will work on union right once you have built in discrimination based on key presence
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
const nameArk = type({ name: "string", age: "number" }).onDeepUndeclaredKey(
"delete"
);
const cityArk = type({ city: "string" }).onDeepUndeclaredKey("delete");
const input = { name: "ark" };
const result = nameArk.or(cityArk)(input)
console.log(result.summary)
const nameArk = type({ name: "string", age: "number" }).onDeepUndeclaredKey(
"delete"
);
const cityArk = type({ city: "string" }).onDeepUndeclaredKey("delete");
const input = { name: "ark" };
const result = nameArk.or(cityArk)(input)
console.log(result.summary)
This prints age must be a number (was missing) or city must be a string (was missing). I just want the error msg to be age must be a number (was missing) to do that I am currently doing something like this
const vm = {
"name": nameArk,
"city": cityArk
};
const input = { name: "ark" };
let result: typeof nameArk.infer | typeof cityArk.infer;
for (const key of keys(vm)) {
if (key in input) {
result = vm[key].assert(input);
break;
}
}
const vm = {
"name": nameArk,
"city": cityArk
};
const input = { name: "ark" };
let result: typeof nameArk.infer | typeof cityArk.infer;
for (const key of keys(vm)) {
if (key in input) {
result = vm[key].assert(input);
break;
}
}
so I wanted to know if there is more idiomatic way to do this
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
@ArkDavid @Dimava the Monoreaper What will be the most idiomatic way to do the validation for such union types to get correct error msg. Do I need to check for the properties and do validation by creating a validator map
import { type } from "arktype";
import { keys } from "remeda";

const nameArk = type({ name: "string" }).onDeepUndeclaredKey("delete");
const cityArk = type({ city: "string" }).onDeepUndeclaredKey("delete");
const vm = {
"name": nameArk,
"city": cityArk
};
const input = { name: "ark" };
let result: typeof nameArk.infer | typeof cityArk.infer;
for (const key of keys(vm)) {
if (key in input) {
result = vm[key].assert(input);
break;
}
}

// peform pipe operation here
import { type } from "arktype";
import { keys } from "remeda";

const nameArk = type({ name: "string" }).onDeepUndeclaredKey("delete");
const cityArk = type({ city: "string" }).onDeepUndeclaredKey("delete");
const vm = {
"name": nameArk,
"city": cityArk
};
const input = { name: "ark" };
let result: typeof nameArk.infer | typeof cityArk.infer;
for (const key of keys(vm)) {
if (key in input) {
result = vm[key].assert(input);
break;
}
}

// peform pipe operation here
This is getting verbose. I am giving example of just one field but my inputs are larger and I have 5 different types of input. If I could have done union then I would have declared on deepUndeclaredkey("delete") on just the union as well as the pipe on the union
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
Oh Yeah, Understood 👍
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
I am getting name must be a string (was missing) or city must be a string (was missing) for both the code snippets below.
const type1 = type({"name": "string",age:"number","+": "delete"})
const type2 = type({"city": "string","+": "delete"})
const union = type([type1,"|",type2])
const result = union({"age":12})
console.log(result.summary)
const type1 = type({"name": "string",age:"number","+": "delete"})
const type2 = type({"city": "string","+": "delete"})
const union = type([type1,"|",type2])
const result = union({"age":12})
console.log(result.summary)
and
const type1 = type({ name: "string", age: "number", "+": "delete" }).pipe(
(o) => {
return { ...o, kind: "person" as const };
}
);
const type2 = type({ city: "string", "+": "delete" }).pipe((o) => {
return { ...o, kind: "city" as const };
});

const union = type([type1, "|", type2]);
const result = union({ age: 12, kind: "city" });
console.log(result.summary);
const type1 = type({ name: "string", age: "number", "+": "delete" }).pipe(
(o) => {
return { ...o, kind: "person" as const };
}
);
const type2 = type({ city: "string", "+": "delete" }).pipe((o) => {
return { ...o, kind: "city" as const };
});

const union = type([type1, "|", type2]);
const result = union({ age: 12, kind: "city" });
console.log(result.summary);
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
@ArkDavid How can I get better error msg for this
const type1 = type({"name": "string",age:"number"})
const type2 = type({"city": "string"})

const union = type([type1,"|",type2])
const result = union({"age":12})
console.log(result.summary)
const type1 = type({"name": "string",age:"number"})
const type2 = type({"city": "string"})

const union = type([type1,"|",type2])
const result = union({"age":12})
console.log(result.summary)
prints "name must be a string (was missing) or city must be a string (was missing)" since age is already there shouldn't it just say "name must be a string (was missing) "
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
docs will certainly help.
66 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
I need to learn a lot 🙂 . This library is just awesome.
66 replies
Aarktype
Created by dibbo on 8/16/2024 in #questions
Latest "keys:distilled" equivalent?
Got it. Thank you
11 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Is there a way to rename the key in parse result
This will work, Thank you
3 replies
Aarktype
Created by dibbo on 8/16/2024 in #questions
Latest "keys:distilled" equivalent?
Great, any reason why and doesn't work but merge works.
11 replies
Aarktype
Created by aabad_ankit on 9/4/2024 in #questions
Can I add a default value to a type if everything other satisfies in a union type
@Dimava the Monoreaper If I try this
const baseType = type({ id: "string" }).pipe((o) => {
return { userId: o.id };
});

const newType = baseType.and({name: "string"})

console.log(newType({name:"ankit",id:"102"}))
const baseType = type({ id: "string" }).pipe((o) => {
return { userId: o.id };
});

const newType = baseType.and({name: "string"})

console.log(newType({name:"ankit",id:"102"}))
1. merge is not available on baseType 2. I am not getting the name in the result, it seems the pipe operation is applied in the end
66 replies
Aarktype
Created by dibbo on 8/16/2024 in #questions
Latest "keys:distilled" equivalent?
@ArkDavid If I add onUndeclaredKey("delete"), I can't further use this type to construct more types const t = type({ a: "string" }).onUndeclaredKey("delete") const t2 = t.and({b:"string"})
11 replies