fix: validate document uploads and allow clearing expiry dates
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
export function decodeDocumentUpload(value) {
|
||||
if (typeof value !== 'string' || !value.length || value.length % 4 !== 0 ||
|
||||
!/^[A-Za-z0-9+/]*={0,2}$/.test(value))
|
||||
throw new Error('Dosya içeriği geçersiz; dosyayı yeniden seçin.');
|
||||
const bytes = Buffer.from(value, 'base64');
|
||||
if (!bytes.length || bytes.toString('base64') !== value) throw new Error('Dosya içeriği boş veya bozuk.');
|
||||
if (bytes.length > 10 * 1024 * 1024) throw new Error('Dosya en fazla 10 MB olabilir.');
|
||||
return bytes;
|
||||
}
|
||||
|
||||
export function documentMetadataPatch(body) {
|
||||
const patch = {};
|
||||
if (Object.hasOwn(body, 'sonKullanma')) {
|
||||
const value = body.sonKullanma;
|
||||
if (value !== null && value !== '') {
|
||||
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(value))
|
||||
throw new Error('Son kullanma tarihi YYYY-AA-GG biçiminde olmalı.');
|
||||
const date = new Date(value + 'T00:00:00Z');
|
||||
if (!Number.isFinite(date.getTime()) || date.toISOString().slice(0,10) !== value)
|
||||
throw new Error('Son kullanma tarihi geçerli bir takvim günü olmalı.');
|
||||
}
|
||||
patch.sonKullanma = value || null;
|
||||
}
|
||||
if (Object.hasOwn(body, 'kategori')) {
|
||||
if (body.kategori !== null && typeof body.kategori !== 'string') throw new Error('Belge kategorisi metin olmalı.');
|
||||
patch.kategori = body.kategori?.trim().slice(0,60) || null;
|
||||
}
|
||||
return patch;
|
||||
}
|
||||
Reference in New Issue
Block a user