Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unicode Latin-1 show non valid characters #186

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 38 additions & 3 deletions Validators/LatinValidators.cs
@@ -1,16 +1,19 @@
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using FluentValidation;
using FluentValidation.Validators;

namespace FatturaElettronica.Validators
{

public abstract class LatinBaseValidator<T> : PropertyValidator
{
private readonly Charsets _charset;


public LatinBaseValidator(Charsets charset)
: base($"Testo contenente caratteri non validi ({(charset == Charsets.BasicLatin ? "Unicode Basic Latin" : "Unicode Latin-1 Supplement")})")
: base($"Testo contenente caratteri non validi ({(charset == Charsets.BasicLatin ? "Unicode Basic Latin" : "Unicode Latin-1 Supplement")}). valori non accettati: {{NonLatinCode}}")
{
_charset = charset;
}
Expand All @@ -30,6 +33,38 @@ protected override bool IsValid(PropertyValidatorContext context)
}
return Regex.Match(context.PropertyValue.ToString(), challenge).Success;
}

protected override void PrepareMessageFormatterForValidationError(PropertyValidatorContext context)
{
base.PrepareMessageFormatterForValidationError(context);

var invalidLetters = new HashSet<char>();
if (context.PropertyValue != null)
{
foreach (var letter in context.PropertyValue.ToString())
{
int upperLimit;
switch (_charset)
{
case Charsets.BasicLatin:
upperLimit = 0x7F;
break;
case Charsets.Latin1Supplement:
upperLimit = 0xFF;
break;
default:
throw new ArgumentOutOfRangeException();
}

if (letter > upperLimit)
{
invalidLetters.Add(letter);
}
}
}

context.MessageFormatter.AppendArgument("NonLatinCode", new string(invalidLetters.ToArray()));
}
}

public class BasicLatinValidator<T> : LatinBaseValidator<T>
Expand Down