From 0c8180d89cf63e60c522ea6f6bc9adacc362eaa5 Mon Sep 17 00:00:00 2001 From: k00b Date: Sun, 27 Oct 2024 21:54:25 -0500 Subject: [PATCH] fix active vault check and optional vaultEntries --- components/vault/use-vault.js | 2 +- lib/yup.js | 26 ++++++++++++++++++-------- wallets/validate.js | 6 +++--- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/components/vault/use-vault.js b/components/vault/use-vault.js index b8333559..d66f8048 100644 --- a/components/vault/use-vault.js +++ b/components/vault/use-vault.js @@ -15,7 +15,7 @@ export default function useVault () { return await decryptValue(key.key, { iv, value }) }, [key]) - return { encrypt, decrypt, isActive: !!key } + return { encrypt, decrypt, isActive: !!key?.key } } /** diff --git a/lib/yup.js b/lib/yup.js index 9984ca03..0571696d 100644 --- a/lib/yup.js +++ b/lib/yup.js @@ -166,24 +166,34 @@ addMethod(string, 'nwcUrl', function () { }) addMethod(array, 'equalto', function equals ( - schemas, + { required, optional }, message ) { return this.test({ name: 'equalto', message: message || `${this.path} has invalid values`, test: function (items) { - if (items.length !== schemas.length) { - return this.createError({ message: `Expected ${this.path} to be ${schemas.length} items, but got ${items.length}` }) + if (items.length < required.length) { + return this.createError({ message: `Expected ${this.path} to be at least ${required.length} items, but got ${items.length}` }) } - const remainingSchemas = [...schemas] + if (items.length > required.length + optional.length) { + return this.createError({ message: `Expected ${this.path} to be at most ${required.length + optional.length} items, but got ${items.length}` }) + } + const remainingRequiredSchemas = [...required] + const remainingOptionalSchemas = [...optional] for (let i = 0; i < items.length; i++) { - const index = remainingSchemas.findIndex(schema => schema.isValidSync(items[i], { strict: true })) - if (index === -1) { - return this.createError({ message: `${this.path}[${i}] has invalid value` }) + const requiredIndex = remainingRequiredSchemas.findIndex(schema => schema.isValidSync(items[i], { strict: true })) + if (requiredIndex === -1) { + const optionalIndex = remainingOptionalSchemas.findIndex(schema => schema.isValidSync(items[i], { strict: true })) + if (optionalIndex === -1) { + return this.createError({ message: `${this.path}[${i}] has invalid value` }) + } + remainingOptionalSchemas.splice(optionalIndex, 1) + continue } - remainingSchemas.splice(index, 1) + remainingRequiredSchemas.splice(requiredIndex, 1) } + return true } }) diff --git a/wallets/validate.js b/wallets/validate.js index b3b3a4da..0e91a559 100644 --- a/wallets/validate.js +++ b/wallets/validate.js @@ -61,13 +61,13 @@ function createFieldSchema (name, validate) { function composeWalletSchema (walletDef, serverSide) { const { fields } = walletDef - const vaultEntrySchemas = [] + const vaultEntrySchemas = { required: [], optional: [] } const schemaShape = fields.reduce((acc, field) => { const { name, validate, optional, clientOnly, requiredWithout } = field if (clientOnly && serverSide) { // For server-side validation, accumulate clientOnly fields as vaultEntries - vaultEntrySchemas.push(vaultEntrySchema(name)) + vaultEntrySchemas[optional ? 'optional' : 'required'].push(vaultEntrySchema(name)) } else { acc[name] = createFieldSchema(name, validate) @@ -88,7 +88,7 @@ function composeWalletSchema (walletDef, serverSide) { }, {}) // Finalize the vaultEntries schema if it exists - if (vaultEntrySchemas.length > 0) { + if (vaultEntrySchemas.required.length > 0 || vaultEntrySchemas.optional.length > 0) { schemaShape.vaultEntries = Yup.array().equalto(vaultEntrySchemas) }