fix active vault check and optional vaultEntries
This commit is contained in:
parent
dce5762f63
commit
0c8180d89c
|
@ -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 }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
26
lib/yup.js
26
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
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue