`.configure` not being applied

Hey all! I'm trying to use .configure to customize the error messages and it seems like it isn't being applied. For instance if you put this in the playground, you'll see that the error message is "Password must contain at least one character that isn't a letter or number". My expectation is that it would either be 'override problem' or 'override message' because that's what I've configured. Additionally, if I add console.log statements to the problem and message functions it looks like those never run. What am I doing wrong?
import { type } from "arktype"

const Thing = type('string >= 12')
.narrow((str, ctx) => {
if (!/[A-Z]/.test(str)) {
ctx.error('Password must contain at least one uppercase letter')
}
if (!/[a-z]/.test(str)) {
ctx.error('Password must contain at least one lowercase letter')
}
if (!/\d/.test(str)) {
ctx.error('Password must contain at least one number')
}
if (!/[^\w\s]/.test(str)) {
ctx.error("Password must contain at least one character that isn't a letter or number")
}
return true
})
.configure({
problem: () => {
return 'override problem'
},
message: () => {
return 'override message'
},
})

const out = Thing("Password1234")
import { type } from "arktype"

const Thing = type('string >= 12')
.narrow((str, ctx) => {
if (!/[A-Z]/.test(str)) {
ctx.error('Password must contain at least one uppercase letter')
}
if (!/[a-z]/.test(str)) {
ctx.error('Password must contain at least one lowercase letter')
}
if (!/\d/.test(str)) {
ctx.error('Password must contain at least one number')
}
if (!/[^\w\s]/.test(str)) {
ctx.error("Password must contain at least one character that isn't a letter or number")
}
return true
})
.configure({
problem: () => {
return 'override problem'
},
message: () => {
return 'override message'
},
})

const out = Thing("Password1234")
6 Replies
ssalbdivad
ssalbdivad5d ago
.configure changes the errors that get created by your types, but if you create custom errors with narrow, it wouldn't affect those. You can see if you shorten the password to be less than 12 characters, which is enforced by the base type, your custom message applies. You want to write your own error message in narrow and then override it again?
nickmazuk
nickmazukOP4d ago
Yeah looks like if I hit the length check I get the overriden message. But I guess I had a few expectations of how things would work after reading the docs (which is not how they're actually working): - Configure would do the same thing for every error. To me configure and narrow should be orthoginal concepts. By making configure not affect narrow, you could have "spooky action at a distance" – and it's not clear from reading the code this should be what happens. - ctx.error messages would be shown to the user exactly as they're written. So without configure, the password strength checks display as "must be Password must contain at least one character that isn't a letter or number (was "assdfdfsd12A1")" in the error summary. My hope here was that because the must be ___ format doesn't work well for these custom checks that I could configure the error messages to show my custom error exactly.
Flosi21
Flosi214d ago
You can actually configure them by returning ctx.reject({ message: "Your message"}) if I am not mistaken Though I agree that intuitively configure should always override narrow 🤔
nickmazuk
nickmazukOP4d ago
Yep that worked, thanks!
ssalbdivad
ssalbdivad4d ago
The error config docs do actually describe how these parts work together: https://arktype.io/docs/configuration#errors The goal of making the more granular config options like expected for must be ____ the defaults is that they can be composed if you e.g. use your type in a union.
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
ssalbdivad
ssalbdivad4d ago
@nickmazuk @Flosi21 .configure basically iterates over each node and adds metadata so that when that specific node creates an error, it will use the associated error config. What's unique about .narrow is that the errors being created aren't attached to the node itself, but created dynamically by the user in the narrow logic. It is impossible to transform them directly since they're not introspectable at runtime.

Did you find this page helpful?