Bobakanoosh
Bobakanoosh
Explore posts from servers
Aarktype
Created by Bobakanoosh on 2/19/2025 in #questions
Early codependent field validation
This is the aforementioned solution of narrowing directly on the dependent field. Probably the best solution I’ve got, though not ideal due to root being unknown and having to validate it myself.
const PasswordSchema = type({
password: "string > 1",
confirmPassword: type("string > 1").narrow((details, ctx) => {
const root = typeof ctx.root === "object" ? (ctx.root as Record<string, unknown>) : null
if (root && root.password !== root.confirmPassword) {
ctx.reject({
expected: "identical to password",
actual: "",
relativePath: ["confirmPassword"],
})
}

return !ctx.hasError()
}),
})

const FormArktype = type({
"...": PasswordSchema,
email: "string.email",
})

const arkResult = FormArktype({ email: "test", password: "2345", confirmPassword: "12" })
if (arkResult instanceof type.errors) {
console.log(arkResult.summary)
}
const PasswordSchema = type({
password: "string > 1",
confirmPassword: type("string > 1").narrow((details, ctx) => {
const root = typeof ctx.root === "object" ? (ctx.root as Record<string, unknown>) : null
if (root && root.password !== root.confirmPassword) {
ctx.reject({
expected: "identical to password",
actual: "",
relativePath: ["confirmPassword"],
})
}

return !ctx.hasError()
}),
})

const FormArktype = type({
"...": PasswordSchema,
email: "string.email",
})

const arkResult = FormArktype({ email: "test", password: "2345", confirmPassword: "12" })
if (arkResult instanceof type.errors) {
console.log(arkResult.summary)
}
4 replies
Aarktype
Created by Bobakanoosh on 2/19/2025 in #questions
Early codependent field validation
This works but not really ideal, especially in the case of a more complex schema
const PasswordSchema = type({
password: "string > 1",
confirm: "string > 1",
}).narrow((details, ctx) => {
if (details.password !== details.confirm) {
ctx.reject({
expected: "identical to password",
actual: "",
relativePath: ["confirm"],
})
}

return !ctx.hasError()
})

const FormArktype = type({
email: "string.email",
password: PasswordSchema,
}).pipe((v) => {
return {
...v,
...v.password,
}
})

const arkResult = FormArktype({ email: "[email protected]", password: { password: "2345", confirm: "2345" } })
if (arkResult instanceof type.errors) {
console.log(arkResult.summary)
} else {
console.log(arkResult)
}
const PasswordSchema = type({
password: "string > 1",
confirm: "string > 1",
}).narrow((details, ctx) => {
if (details.password !== details.confirm) {
ctx.reject({
expected: "identical to password",
actual: "",
relativePath: ["confirm"],
})
}

return !ctx.hasError()
})

const FormArktype = type({
email: "string.email",
password: PasswordSchema,
}).pipe((v) => {
return {
...v,
...v.password,
}
})

const arkResult = FormArktype({ email: "[email protected]", password: { password: "2345", confirm: "2345" } })
if (arkResult instanceof type.errors) {
console.log(arkResult.summary)
} else {
console.log(arkResult)
}
4 replies
Aarktype
Created by Bobakanoosh on 2/19/2025 in #questions
Early codependent field validation
Was hoping I could do something like this:
const PasswordSchema = type({
password: "string > 1",
confirm: "string > 1",
}).narrow((details, ctx) => {
if (details.password !== details.confirm) {
ctx.reject({
expected: "identical to password",
actual: "",
relativePath: ["confirm"],
})
}

return !ctx.hasError()
})

const FormArktype = type({
"...": PasswordSchema,
email: "string.email",
})
const PasswordSchema = type({
password: "string > 1",
confirm: "string > 1",
}).narrow((details, ctx) => {
if (details.password !== details.confirm) {
ctx.reject({
expected: "identical to password",
actual: "",
relativePath: ["confirm"],
})
}

return !ctx.hasError()
})

const FormArktype = type({
"...": PasswordSchema,
email: "string.email",
})
4 replies
Aarktype
Created by Bobakanoosh on 1/30/2025 in #questions
Easier way to chain off of string.integer.parse?
I think i saw a diff example that was .to. and i tried that and saw nothing useful to chain off that
9 replies
Aarktype
Created by Bobakanoosh on 1/30/2025 in #questions
Easier way to chain off of string.integer.parse?
😂 not completely convinced its not true..
9 replies
Aarktype
Created by Bobakanoosh on 1/30/2025 in #questions
Easier way to chain off of string.integer.parse?
thanks!
9 replies
Aarktype
Created by Bobakanoosh on 1/30/2025 in #questions
Easier way to chain off of string.integer.parse?
I SWEAR i tried that 😭 😭
9 replies
Aarktype
Created by Maxzilla on 1/29/2025 in #questions
Zod "parity"? (nanoid, emoji,...)
Beautiful, thanks!
15 replies
Aarktype
Created by Maxzilla on 1/29/2025 in #questions
Zod "parity"? (nanoid, emoji,...)
So there’s no pitfall/repercussions of using a global scope for that purpose? Just wanting to make sure if I go that route I’m not gonna be 3 months down the road and realize some validation isn’t gonna work because I’m using that global scope. Maybe a paranoid thought
15 replies
Aarktype
Created by Maxzilla on 1/29/2025 in #questions
Zod "parity"? (nanoid, emoji,...)
Awesome, I find some libraries are opinionated and it’s easier to follow those opinions rather than fight them, so was just checking.
15 replies
Aarktype
Created by Maxzilla on 1/29/2025 in #questions
Zod "parity"? (nanoid, emoji,...)
@ArkDavid so is your recommendation to have one big global scope that all schemas use, which defines custom types, as opposed to creating types and reusing those? Ex:
const {type } = scope({
"string.emoji": arkType("string").narrow((str) => /* Placeholder */ true)
})

const stringEmoji = arkType("string").narrow((str) => /* Placeholder */ true)

const someSchema = type({
input: stringEmoji,
output: "string.emoji",
})
const {type } = scope({
"string.emoji": arkType("string").narrow((str) => /* Placeholder */ true)
})

const stringEmoji = arkType("string").narrow((str) => /* Placeholder */ true)

const someSchema = type({
input: stringEmoji,
output: "string.emoji",
})
15 replies
Aarktype
Created by Bobakanoosh on 1/28/2025 in #questions
How can I globally configure a subtypes error
Awesome thanks again!!
32 replies
Aarktype
Created by Bobakanoosh on 1/28/2025 in #questions
How can I globally configure a subtypes error
Will watch that ticket and use the helper method in the meantime
32 replies
Aarktype
Created by Bobakanoosh on 1/28/2025 in #questions
How can I globally configure a subtypes error
awesome
32 replies
Aarktype
Created by Bobakanoosh on 1/28/2025 in #questions
How can I globally configure a subtypes error
that’s true yeah
32 replies
Aarktype
Created by Bobakanoosh on 1/28/2025 in #questions
How can I globally configure a subtypes error
Yeah fair that does seem very ideal
32 replies
Aarktype
Created by Bobakanoosh on 1/28/2025 in #questions
How can I globally configure a subtypes error
But “the Zod way” is adding a new thing in the same “code path” that already exists for configuration
32 replies
Aarktype
Created by Bobakanoosh on 1/28/2025 in #questions
How can I globally configure a subtypes error
Sure yeah they both add new things
32 replies
Aarktype
Created by Bobakanoosh on 1/28/2025 in #questions
How can I globally configure a subtypes error
Not sure how complex that would be to add to the error object
32 replies
Aarktype
Created by Bobakanoosh on 1/28/2025 in #questions
How can I globally configure a subtypes error
then you could just say if(err.code === “pattern” && err.subtype === “email”) within the already existing configuration system
32 replies