The minimalistic document archiving platform.
import { Buffer } from 'node:buffer';
import * as v from 'valibot';
import { areStorageKeyEncryptionKeysUnique } from './storage-encryption.schemas.models';
const storageKeyEncryptionKeySchema = v.object({
version: v.string(),
key: v.pipe(v.instance(Buffer), v.length(32)),
});
const storageKeyEncryptionKeyListSchema = v.pipe(
v.array(storageKeyEncryptionKeySchema),
v.check((keys) => areStorageKeyEncryptionKeysUnique(keys), 'The keys must have unique versions'),
);
const versionedHexKeySchema = v.pipe(
v.string(),
v.transform((x): { version?: string; key?: string } => {
if (!x.includes(':')) {
return { version: '1', key: x };
}
const [version, key] = x.split(':');
return { version, key };
}),
v.object({
version: v.string(),
key: v.pipe(
v.string(),
v.regex(/^[0-9a-f]{64}$/i, 'The key must be a 64 hex characters string (32 bytes)'),
),
}),
v.transform(({ version, key }) => ({
version,
key: Buffer.from(key, 'hex'),
})),
);
export const coercedStorageKeyEncryptionKeysListSchema = v.optional(
v.union(
[
storageKeyEncryptionKeyListSchema,
v.pipe(
v.string(),
v.transform((value) => value.split(',').map((part) => part.trim())),
v.array(versionedHexKeySchema),
v.check(
(keys) => areStorageKeyEncryptionKeysUnique(keys),
'The keys must have unique versions',
),
),
],
'The value must be either a 32 bytes long hex string, or a comma separated list of version:key pairs where the key is a 32 bytes long hex string',
),
);