validate method
Future<String?>
validate(
- String text,
- CdxCommentsLocalizations loc, {
- int maxLines = 10,
- int maxLength = 400,
Validates comment text according to all rules.
Checks the text for:
- Empty content
- Maximum line count (default: 10)
- Maximum character length (default: 400)
- Dangerous content (XSS, SQL injection, etc.)
- Bad words
text is the comment text to validate.
loc is the CdxCommentsLocalizations instance for error messages.
maxLines is the maximum number of lines allowed (default: 10).
maxLength is the maximum character length allowed (default: 400).
Returns a localized error message if validation fails, or null if valid.
Example:
final error = await validator.validate(
'This is a comment',
localizations,
maxLines: 5,
maxLength: 200,
);
if (error != null) {
print('Validation error: $error');
}
Implementation
Future<String?> validate(String text, CdxCommentsLocalizations loc, {int maxLines = 10, int maxLength = 400}) async {
if (text.trim().isEmpty) return loc.comment_empty;
if (text.split('\n').length > maxLines) {
return loc.comment_too_many_lines(maxLines);
}
if (text.length > maxLength) {
return loc.comment_too_long(maxLength);
}
if (containsDangerousContent(text)) {
return loc.comment_dangerous;
}
if (await containsBadWords(text)) {
return loc.comment_bad_words;
}
return null;
}