முக்கிய உள்ளடக்கத்திற்குச் செல்

தனிப்பயன் சரிபார்ப்பு

உங்கள் பதிவேற்றங்களுக்கு அதிநுட்பமான சரிபார்ப்பு விதிகளை உருவாக்கவும்.

அடிப்படை தனிப்பயன் விதிகள்

import { useDropup } from '@samithahansaka/dropup';

function CustomValidationUploader() {
const { files, getDropProps, getInputProps } = useDropup({
customRules: [
// விதி 1: கோப்பு பெயரைச் சரிபார்
(file) => {
if (file.name.includes(' ')) {
return 'கோப்பு பெயர்களில் இடைவெளிகள் இருக்கக்கூடாது';
}
return true;
},

// விதி 2: நீட்டிப்பைச் சரிபார்
(file) => {
const ext = file.name.split('.').pop()?.toLowerCase();
const blocked = ['exe', 'bat', 'cmd', 'sh'];
if (ext && blocked.includes(ext)) {
return 'இயங்கக்கூடிய கோப்புகள் அனுமதிக்கப்படவில்லை';
}
return true;
},
],

onValidationError: (errors) => {
errors.forEach(({ file, errors }) => {
alert(`${file.name}:\n${errors.join('\n')}`);
});
},
});

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>கோப்புகளை இங்கே விடுங்கள் (பெயர்களில் இடைவெளிகள் இல்லை, இயங்கக்கூடியவை இல்லை)</p>
</div>
);
}

ஒத்திசைவற்ற சரிபார்ப்பு

import { useDropup } from '@samithahansaka/dropup';

function AsyncValidationUploader() {
const { files, getDropProps, getInputProps } = useDropup({
customRules: [
// சேவையகத்தில் நகலைச் சரிபார்
async (file) => {
const hash = await calculateHash(file);
const response = await fetch('/api/check-duplicate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ hash, filename: file.name }),
});
const { exists } = await response.json();

if (exists) {
return 'இந்த கோப்பு ஏற்கனவே பதிவேற்றப்பட்டுள்ளது';
}
return true;
},

// கோப்பு உள்ளடக்கத்தை சரிபார்
async (file) => {
if (file.type === 'application/json') {
const text = await file.text();
try {
JSON.parse(text);
return true;
} catch {
return 'தவறான JSON கோப்பு';
}
}
return true;
},
],
});

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>கோப்புகளை இங்கே விடுங்கள் (சேவையகத்தில் சரிபார்க்கப்படும்)</p>
</div>
);
}

// கோப்பு ஹாஷ் கணக்கிட உதவி செயல்பாடு
async function calculateHash(file: File): Promise<string> {
const buffer = await file.arrayBuffer();
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}

பட உள்ளடக்க சரிபார்ப்பு

import { useDropup } from '@samithahansaka/dropup';

function ImageContentValidator() {
const { files, getDropProps, getInputProps } = useDropup({
accept: 'image/*',

customRules: [
// உண்மையான பட பரிமாணங்களைச் சரிபார்
async (file) => {
if (!file.type.startsWith('image/')) return true;

const dimensions = await getImageDimensions(file);

if (dimensions.width < 200 || dimensions.height < 200) {
return 'படம் குறைந்தபட்சம் 200x200 பிக்சல்களாக இருக்க வேண்டும்';
}

if (dimensions.width > 4000 || dimensions.height > 4000) {
return 'படம் 4000x4000 பிக்சல்களை மீறக்கூடாது';
}

return true;
},

// விகித விகிதத்தைச் சரிபார்
async (file) => {
if (!file.type.startsWith('image/')) return true;

const { width, height } = await getImageDimensions(file);
const ratio = width / height;

// தோராயமாக சதுர படங்கள் தேவை (0.8 to 1.2 விகிதம்)
if (ratio < 0.8 || ratio > 1.2) {
return 'படம் தோராயமாக சதுரமாக இருக்க வேண்டும் (விகித விகிதம் 0.8-1.2)';
}

return true;
},
],
});

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>சதுர படங்களை விடுங்கள் (குறைந்தபட்சம் 200px, அதிகபட்சம் 4000px)</p>

<div style={styles.gallery}>
{files.map(file => (
<img
key={file.id}
src={file.preview}
alt=""
style={styles.preview}
/>
))}
</div>
</div>
);
}

// பட பரிமாணங்களைப் பெற உதவி
function getImageDimensions(file: File): Promise<{ width: number; height: number }> {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
resolve({ width: img.width, height: img.height });
URL.revokeObjectURL(img.src);
};
img.onerror = reject;
img.src = URL.createObjectURL(file);
});
}

const styles = {
dropzone: {
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center' as const,
},
gallery: {
display: 'flex',
gap: 8,
marginTop: 16,
justifyContent: 'center',
},
preview: {
width: 80,
height: 80,
objectFit: 'cover' as const,
borderRadius: 4,
},
};

சார்புநிலைகளுடன் சரிபார்ப்பு

import { useDropup } from '@samithahansaka/dropup';
import { useState } from 'react';

function ConditionalValidation() {
const [category, setCategory] = useState('image');

const { files, getDropProps, getInputProps } = useDropup({
// வகையின் அடிப்படையில் மாறும் accept
accept: category === 'image'
? 'image/*'
: category === 'document'
? '.pdf,.doc,.docx'
: '*/*',

customRules: [
(file) => {
// வகை-குறிப்பிட்ட சரிபார்ப்பு
if (category === 'image') {
if (!file.type.startsWith('image/')) {
return 'ஒரு படக் கோப்பைப் பதிவேற்றவும்';
}
if (file.size > 5 * 1024 * 1024) {
return 'படங்கள் 5MB க்குக் கீழே இருக்க வேண்டும்';
}
}

if (category === 'document') {
if (file.size > 10 * 1024 * 1024) {
return 'ஆவணங்கள் 10MB க்குக் கீழே இருக்க வேண்டும்';
}
}

return true;
},
],
});

return (
<div>
<div style={{ marginBottom: 20 }}>
<label>
<input
type="radio"
value="image"
checked={category === 'image'}
onChange={() => setCategory('image')}
/>
படங்கள் (அதிகபட்சம் 5MB)
</label>
<label style={{ marginLeft: 20 }}>
<input
type="radio"
value="document"
checked={category === 'document'}
onChange={() => setCategory('document')}
/>
ஆவணங்கள் (அதிகபட்சம் 10MB)
</label>
</div>

<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>
{category === 'image' ? 'படங்களை' : 'ஆவணங்களை'} இங்கே விடுங்கள்
</p>
</div>
</div>
);
}

சரிபார்ப்பு பிழை காட்சி

import { useDropup } from '@samithahansaka/dropup';
import { useState } from 'react';

type ValidationErrorType = { file: File; errors: string[] };

function ValidationErrorDisplay() {
const [validationErrors, setValidationErrors] = useState<ValidationErrorType[]>([]);

const { files, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
maxSize: 5 * 1024 * 1024,
maxFiles: 3,

customRules: [
(file) => {
if (file.name.length > 50) {
return 'கோப்பு பெயர் மிக நீளமானது (அதிகபட்சம் 50 எழுத்துகள்)';
}
return true;
},
],

onValidationError: (errors) => {
setValidationErrors(errors);

// 5 வினாடிகளுக்குப் பிறகு அழி
setTimeout(() => setValidationErrors([]), 5000);
},
});

return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>3 படங்கள் வரை விடுங்கள் (ஒவ்வொன்றும் அதிகபட்சம் 5MB)</p>
</div>

{/* சரிபார்ப்பு பிழைகள் */}
{validationErrors.length > 0 && (
<div style={styles.errorContainer}>
<h4 style={styles.errorTitle}>சில கோப்புகள் நிராகரிக்கப்பட்டன:</h4>
{validationErrors.map(({ file, errors }, index) => (
<div key={index} style={styles.errorItem}>
<strong>{file.name}</strong>
<ul style={styles.errorList}>
{errors.map((error, i) => (
<li key={i}>{error}</li>
))}
</ul>
</div>
))}
</div>
)}

{/* ஏற்றுக்கொள்ளப்பட்ட கோப்புகள் */}
{files.length > 0 && (
<div style={styles.acceptedFiles}>
<h4>ஏற்றுக்கொள்ளப்பட்ட கோப்புகள்:</h4>
{files.map(file => (
<div key={file.id}>{file.name}</div>
))}
</div>
)}
</div>
);
}

const styles = {
dropzone: {
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center' as const,
},
errorContainer: {
marginTop: 16,
padding: 16,
backgroundColor: '#ffebee',
borderRadius: 8,
border: '1px solid #f44336',
},
errorTitle: {
color: '#c62828',
margin: '0 0 12px',
},
errorItem: {
marginBottom: 8,
},
errorList: {
margin: '4px 0 0',
paddingLeft: 20,
color: '#c62828',
},
acceptedFiles: {
marginTop: 16,
padding: 16,
backgroundColor: '#e8f5e9',
borderRadius: 8,
},
};