fix active vault check and optional vaultEntries

This commit is contained in:
k00b 2024-10-27 21:54:25 -05:00
parent dce5762f63
commit 0c8180d89c
3 changed files with 22 additions and 12 deletions

View File

@ -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 }
}
/**

View File

@ -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
}
})

View File

@ -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)
}