2020-06-01 02:12:50 +01:00
|
|
|
/******************************************************************************
|
|
|
|
|
// MARECHAI: Master repository of computing history artifacts information
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as
|
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// Copyright © 2003-2020 Natalia Portillo
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
2020-05-27 02:48:44 +01:00
|
|
|
using System;
|
2020-05-27 15:01:55 +01:00
|
|
|
using System.Text.RegularExpressions;
|
2020-05-27 02:48:44 +01:00
|
|
|
using Blazorise;
|
2020-05-27 15:01:55 +01:00
|
|
|
using Match = System.Text.RegularExpressions.Match;
|
2020-05-27 02:48:44 +01:00
|
|
|
|
|
|
|
|
namespace Marechai.Shared
|
|
|
|
|
{
|
|
|
|
|
public static class Validators
|
|
|
|
|
{
|
2020-05-27 15:01:55 +01:00
|
|
|
const string _urlRegex =
|
|
|
|
|
@"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$";
|
|
|
|
|
|
2020-05-27 05:29:37 +01:00
|
|
|
public static void ValidateString(ValidatorEventArgs e, string message, int maxLength)
|
2020-05-27 02:48:44 +01:00
|
|
|
{
|
|
|
|
|
string item = e.Value as string;
|
|
|
|
|
|
|
|
|
|
if(item?.Length > maxLength)
|
|
|
|
|
{
|
|
|
|
|
e.ErrorText = message;
|
|
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
e.Status = string.IsNullOrWhiteSpace(item) ? ValidationStatus.Error : ValidationStatus.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ValidateIntroducedDate(ValidatorEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(!(e.Value is DateTime item) ||
|
|
|
|
|
item.Year < 1900 ||
|
|
|
|
|
item.Date >= DateTime.UtcNow.Date)
|
|
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
else
|
|
|
|
|
e.Status = ValidationStatus.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 05:29:37 +01:00
|
|
|
public static void ValidateDouble(ValidatorEventArgs e, double minValue = 0, double maxValue = double.MaxValue)
|
2020-05-27 02:48:44 +01:00
|
|
|
{
|
|
|
|
|
if(!(e.Value is double item) ||
|
2020-05-27 15:01:55 +01:00
|
|
|
item < minValue ||
|
|
|
|
|
item > maxValue)
|
2020-05-27 02:48:44 +01:00
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
else
|
|
|
|
|
e.Status = ValidationStatus.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 05:29:37 +01:00
|
|
|
public static void ValidateInteger(ValidatorEventArgs e, int minValue = 0, int maxValue = int.MaxValue)
|
2020-05-27 02:48:44 +01:00
|
|
|
{
|
|
|
|
|
if(!(e.Value is int item) ||
|
2020-05-27 15:01:55 +01:00
|
|
|
item < minValue ||
|
|
|
|
|
item > maxValue)
|
2020-05-27 03:48:34 +01:00
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
else
|
|
|
|
|
e.Status = ValidationStatus.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 05:29:37 +01:00
|
|
|
public static void ValidateLong(ValidatorEventArgs e, long minValue = 0, long maxValue = long.MaxValue)
|
2020-05-27 03:48:34 +01:00
|
|
|
{
|
|
|
|
|
if(!(e.Value is long item) ||
|
2020-05-27 15:01:55 +01:00
|
|
|
item < minValue ||
|
|
|
|
|
item > maxValue)
|
2020-05-27 02:48:44 +01:00
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
else
|
|
|
|
|
e.Status = ValidationStatus.Success;
|
|
|
|
|
}
|
2020-05-27 05:29:37 +01:00
|
|
|
|
|
|
|
|
public static void ValidateFloat(ValidatorEventArgs e, float minValue = 0, float maxValue = float.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
if(!(e.Value is float item) ||
|
2020-05-27 15:01:55 +01:00
|
|
|
item < minValue ||
|
|
|
|
|
item > maxValue)
|
2020-05-27 05:29:37 +01:00
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
else
|
|
|
|
|
e.Status = ValidationStatus.Success;
|
|
|
|
|
}
|
2020-05-27 15:01:55 +01:00
|
|
|
|
|
|
|
|
public static void ValidateUrl(ValidatorEventArgs e, string message, int maxLength)
|
|
|
|
|
{
|
|
|
|
|
if(!(e.Value is string url))
|
|
|
|
|
{
|
|
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(url.Length < 1 ||
|
|
|
|
|
url.Length > maxLength)
|
|
|
|
|
{
|
|
|
|
|
e.ErrorText = message;
|
|
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var rx = new Regex(_urlRegex);
|
|
|
|
|
Match m = rx.Match(url);
|
|
|
|
|
|
|
|
|
|
if(m.Success)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
}
|
2020-05-27 21:24:53 +01:00
|
|
|
|
|
|
|
|
public static void ValidateDate(ValidatorEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(!(e.Value is DateTime item) ||
|
|
|
|
|
item.Date >= DateTime.UtcNow.Date)
|
|
|
|
|
e.Status = ValidationStatus.Error;
|
|
|
|
|
else
|
|
|
|
|
e.Status = ValidationStatus.Success;
|
|
|
|
|
}
|
2020-05-27 02:48:44 +01:00
|
|
|
}
|
|
|
|
|
}
|