diff --git a/lib/wallet.js b/lib/wallet.js index a83013ff..176d49f8 100644 --- a/lib/wallet.js +++ b/lib/wallet.js @@ -19,9 +19,6 @@ export function generateSchema (wallet) { return validator } - // complex validation - if (field.validate.schema) return field.validate.schema - const { type: validationType, words, min, max } = field.validate let validator diff --git a/wallets/nwc/index.js b/wallets/nwc/index.js index 13e06d33..51639ce8 100644 --- a/wallets/nwc/index.js +++ b/wallets/nwc/index.js @@ -10,29 +10,28 @@ export const fields = [ label: 'connection', type: 'password', validate: { - schema: 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 + type: 'string', + 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 { - 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 { + ({ 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 }) + throw new Error('pubkey must be 64 hex chars') } - return true - }) + 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 + } } } ]