-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate pubkey, relay URL and secret of NWC URL #810
Conversation
I'd guess you'll want something like this but I haven't used their transform yet so not sure: export const nwcSchema = object({
nwc: string().required('required').transform(url => ({ url, ...parseNwcUrl(url) })).object({
nwcUrl: string().required('required').trim().matches(/^nostr\+walletconnect:\/\//),
relayUrl: string().required('relay url required').trim().wss('relay must use wss://'),
walletPubkey: string().required('pubkey required').trim().matches(NOSTR_PUBKEY_HEX, 'pubkey must be 64 hex chars'),
secret: string().required('secret required').trim().matches(/^[0-9a-z]{64}$/, 'secret must be 64 hex chars')
})
}) |
a6c9eec
to
d813290
Compare
Was able to use transform but still had to cast validation errors into the shape formik expects. So couldn't use However, even then, I still had problems with the order of validation (see jquense/yup#851), so I decided to not use transform at all and instead do this: export const nwcSchema = object({
nwcUrl: string()
.required('required')
.test(async (nwcUrl, context) => {
// run validation in sequence to control order of errors
// inspired by https://github.com/jquense/yup/issues/851#issuecomment-1049705180
try {
await string().required('required').validate(nwcUrl)
await string().matches(/^nostr\+?walletconnect:\/\//, { message: 'must start with nostr+walletconnect://' }).validate(nwcUrl)
let relayUrl, walletPubkey, secret
try {
({ relayUrl, walletPubkey, secret } = parseNwcUrl(nwcUrl))
} catch {
// invalid URL error. handle as if pubkey validation failed to not confuse user.
throw new Error('pubkey must be 64 hex chars')
}
await string().required('pubkey required').trim().matches(NOSTR_PUBKEY_HEX, 'pubkey must be 64 hex chars').validate(walletPubkey)
await string().required('relay url required').trim().wss('relay must use wss://').validate(relayUrl)
await string().required('secret required').trim().matches(/^[0-9a-fA-F]{64}$/, 'secret must be 64 hex chars').validate(secret)
} catch (err) {
return context.createError({ message: err.message })
}
return true
})
}) I think this is also clearer now and we can stay consistent and also use |
2292373
to
e2ed7e1
Compare
Thread explaining why transforms don't work: How does yup's trim work? Formik doesn't use the returned object from yup which contains the transforms. According to that thread, we'd just need to add this line to onSubmit={(values) => {
const castValues = mySchema.cast(values)
...
}) |
But we would still need to "cast back" manually to properly show error messages for the input field named |
Ah I see. Still, might be useful for transforms like |
Mhh, wasn't able to use
Yup.transform
so we can keep usingvalidationSchema
. I tried stuff along these lines:but in any case, it threw errors for fields that don't exist (
relayUrl
,walletPubkey
,secret
) which makes sense. So not sure if we can usetransform
to transform a string into an object but still pretend it's the same string we are validating?2024-02-12.01-59-36.mp4