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 }) return await decryptValue(key.key, { iv, value })
}, [key]) }, [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 ( addMethod(array, 'equalto', function equals (
schemas, { required, optional },
message message
) { ) {
return this.test({ return this.test({
name: 'equalto', name: 'equalto',
message: message || `${this.path} has invalid values`, message: message || `${this.path} has invalid values`,
test: function (items) { test: function (items) {
if (items.length !== schemas.length) { if (items.length < required.length) {
return this.createError({ message: `Expected ${this.path} to be ${schemas.length} items, but got ${items.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++) { for (let i = 0; i < items.length; i++) {
const index = remainingSchemas.findIndex(schema => schema.isValidSync(items[i], { strict: true })) const requiredIndex = remainingRequiredSchemas.findIndex(schema => schema.isValidSync(items[i], { strict: true }))
if (index === -1) { if (requiredIndex === -1) {
return this.createError({ message: `${this.path}[${i}] has invalid value` }) 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 return true
} }
}) })

View File

@ -61,13 +61,13 @@ function createFieldSchema (name, validate) {
function composeWalletSchema (walletDef, serverSide) { function composeWalletSchema (walletDef, serverSide) {
const { fields } = walletDef const { fields } = walletDef
const vaultEntrySchemas = [] const vaultEntrySchemas = { required: [], optional: [] }
const schemaShape = fields.reduce((acc, field) => { const schemaShape = fields.reduce((acc, field) => {
const { name, validate, optional, clientOnly, requiredWithout } = field const { name, validate, optional, clientOnly, requiredWithout } = field
if (clientOnly && serverSide) { if (clientOnly && serverSide) {
// For server-side validation, accumulate clientOnly fields as vaultEntries // For server-side validation, accumulate clientOnly fields as vaultEntries
vaultEntrySchemas.push(vaultEntrySchema(name)) vaultEntrySchemas[optional ? 'optional' : 'required'].push(vaultEntrySchema(name))
} else { } else {
acc[name] = createFieldSchema(name, validate) acc[name] = createFieldSchema(name, validate)
@ -88,7 +88,7 @@ function composeWalletSchema (walletDef, serverSide) {
}, {}) }, {})
// Finalize the vaultEntries schema if it exists // 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) schemaShape.vaultEntries = Yup.array().equalto(vaultEntrySchemas)
} }