From 3fb601d14354c867e1ead94b027c99c4a2fc15b5 Mon Sep 17 00:00:00 2001 From: vman Date: Wed, 16 Nov 2022 14:07:28 -0500 Subject: Add project files. --- .../VNLib.Plugins.Extensions.Validation.csproj | 33 ++++ .../VNLib.Plugins.Extensions.Validation.xml | 134 +++++++++++++++ .../ValErrWebMessage.cs | 17 ++ .../ValidationErrorMessage.cs | 12 ++ .../ValidationExtensions.cs | 54 ++++++ .../ValidatorExtensions.cs | 181 +++++++++++++++++++++ 6 files changed, 431 insertions(+) create mode 100644 VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.csproj create mode 100644 VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.xml create mode 100644 VNLib.Plugins.Extensions.Validation/ValErrWebMessage.cs create mode 100644 VNLib.Plugins.Extensions.Validation/ValidationErrorMessage.cs create mode 100644 VNLib.Plugins.Extensions.Validation/ValidationExtensions.cs create mode 100644 VNLib.Plugins.Extensions.Validation/ValidatorExtensions.cs (limited to 'VNLib.Plugins.Extensions.Validation') diff --git a/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.csproj b/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.csproj new file mode 100644 index 0000000..4ffeb9d --- /dev/null +++ b/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.csproj @@ -0,0 +1,33 @@ + + + + net6.0 + AnyCPU;x64 + Vaughn Nugent + Copyright © 2022 Vaughn Nugent + https://www.vaughnnugent.com/resources + 1.0.0.1 + True + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.xml b/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.xml new file mode 100644 index 0000000..a5ebbcb --- /dev/null +++ b/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.xml @@ -0,0 +1,134 @@ + + + + VNLib.Plugins.Extensions.Validation + + + + + Extends the class with provisions for a collection of validations + + + + + A collection of error messages to send to clients + + + + + Defines extenstion methods for + + + + + Gets a collection of Json-serializable validation errors + + + A collection of json errors to return to a user + + + + Tests the the property against + to determine if the string matches the proper phone number form + + + + + + + + Tests the the property against + to determine if the string matches the proper phone number form, or allows emtpy strings + + + + + + + + Checks a string against . + If the string is null or empty, it is allowed. + + + + + + + + Checks a string against . + If the string is null or empty, it is allowed. + + + + + + + + Makes sure a field contains at least 1 character a-Z + + + + + + + + Determines if all characters are only a-Z (allows whitespace) + + + + + + + + Makes sure a field contains at least 1 numeral + + + + + + + + Determines if all characters are only 0-9 (not whitespace is allowed) + + + + + + + + Makes sure the field contains at least 1 alpha numeric character (whitespace included) + + + + + + + + Determines if all characters are only alpha-numeric (whitespace allowed) + + + + + + + + Tests the string against the password regular expression to determine if the + value meets the basic password requirements + + + + + + + + Defines a length validator on the current rule builder, but only for string properties. + Validation will fail if the length of the string is outside of the specified range. + The range is inclusive + + + + The length range of the specified string + + + + diff --git a/VNLib.Plugins.Extensions.Validation/ValErrWebMessage.cs b/VNLib.Plugins.Extensions.Validation/ValErrWebMessage.cs new file mode 100644 index 0000000..118397a --- /dev/null +++ b/VNLib.Plugins.Extensions.Validation/ValErrWebMessage.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Text.Json.Serialization; + +namespace VNLib.Plugins.Extensions.Validation +{ + /// + /// Extends the class with provisions for a collection of validations + /// + public class ValErrWebMessage:WebMessage + { + /// + /// A collection of error messages to send to clients + /// + [JsonPropertyName("errors")] + public ICollection Errors { get; set; } + } +} diff --git a/VNLib.Plugins.Extensions.Validation/ValidationErrorMessage.cs b/VNLib.Plugins.Extensions.Validation/ValidationErrorMessage.cs new file mode 100644 index 0000000..f7130c4 --- /dev/null +++ b/VNLib.Plugins.Extensions.Validation/ValidationErrorMessage.cs @@ -0,0 +1,12 @@ +using System.Text.Json.Serialization; + +namespace VNLib.Plugins.Extensions.Validation +{ + public class ValidationErrorMessage + { + [JsonPropertyName("property")] + public string PropertyName { get; set; } + [JsonPropertyName("message")] + public string ErrorMessage { get; set; } + } +} diff --git a/VNLib.Plugins.Extensions.Validation/ValidationExtensions.cs b/VNLib.Plugins.Extensions.Validation/ValidationExtensions.cs new file mode 100644 index 0000000..fd7880e --- /dev/null +++ b/VNLib.Plugins.Extensions.Validation/ValidationExtensions.cs @@ -0,0 +1,54 @@ +using System; +using System.Diagnostics.CodeAnalysis; + +using FluentValidation; +using FluentValidation.Results; + +#nullable enable + +namespace VNLib.Plugins.Extensions.Validation +{ + /// + /// Provides shortcut methods to aid programmatic validation of objects + /// + public static class ValidationExtensions + { + /// + /// If evalues to false, sets the specified assertion message + /// to the to the specified string + /// + /// + /// The result of the assertion + /// The error message to store when the value is false + /// The inverse of + public static bool Assert(this WebMessage webm, [DoesNotReturnIf(false)] bool assertion, string message) + { + if(!assertion) + { + webm.Success = false; + webm.Result = message; + } + return !assertion; + } + /// + /// Validates the specified instance, and stores errors to the specified + /// and sets the + /// + /// The instance to validate + /// + /// The to store errors to + /// True if the result of the validation is valid, false otherwise and the is not modified + public static bool Validate(this IValidator validator, T instance, ValErrWebMessage webMessage) + { + //Validate value + ValidationResult result = validator.Validate(instance); + //If not valid, set errors on web message + if (!result.IsValid) + { + webMessage.Success = false; + webMessage.Errors = result.GetErrorsAsCollection(); + } + return result.IsValid; + } + } +} diff --git a/VNLib.Plugins.Extensions.Validation/ValidatorExtensions.cs b/VNLib.Plugins.Extensions.Validation/ValidatorExtensions.cs new file mode 100644 index 0000000..172074d --- /dev/null +++ b/VNLib.Plugins.Extensions.Validation/ValidatorExtensions.cs @@ -0,0 +1,181 @@ +using System; +using System.Collections; +using System.Text.RegularExpressions; + +using FluentValidation; +using FluentValidation.Results; + +namespace VNLib.Plugins.Extensions.Validation +{ + /// + /// Defines extenstion methods for + /// + public static class ValidatorExtensions + { + public static readonly Regex PhoneRegex = new(@"^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$", RegexOptions.Compiled); + + public static readonly Regex AlphaRegx = new(@"[a-zA-Z]*", RegexOptions.Compiled); + public static readonly Regex NumericRegx = new(@"[0-9]*", RegexOptions.Compiled); + public static readonly Regex AlphaNumRegx = new(@"[a-zA-Z0-9]*", RegexOptions.Compiled); + + public static readonly Regex OnlyAlphaRegx = new(@"^[a-zA-Z\s]*$", RegexOptions.Compiled); + public static readonly Regex OnlyNumericRegx = new(@"^[0-9]*$", RegexOptions.Compiled); + public static readonly Regex OnlyAlphaNumRegx = new(@"^[a-zA-Z0-9\s]*$", RegexOptions.Compiled); + + public static readonly Regex PasswordRegx = new(@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$ %^&*-])", RegexOptions.Compiled); + public static readonly Regex IllegalRegx = new(@"[\r\n\t\a\b\e\f|^~`<>{}]", RegexOptions.Compiled); + public static readonly Regex SpecialCharactersRegx = new(@"[\r\n\t\a\b\e\f#?!@$%^&*\+\-\~`|<>\{}]", RegexOptions.Compiled); + + + /// + /// Gets a collection of Json-serializable validation errors + /// + /// + /// A collection of json errors to return to a user + public static ICollection GetErrorsAsCollection(this ValidationResult result) + { + return result.Errors.ConvertAll(static err => new ValidationErrorMessage { ErrorMessage = err.ErrorMessage, PropertyName = err.PropertyName }); + } + + /// + /// Tests the the property against + /// to determine if the string matches the proper phone number form + /// + /// + /// + /// + public static IRuleBuilderOptions PhoneNumber(this IRuleBuilder builder) + { + return builder.Must(static phone => phone?.Length > 0 && PhoneRegex.IsMatch(phone)) + .WithMessage("{PropertyValue} is not a valid phone number."); + } + /// + /// Tests the the property against + /// to determine if the string matches the proper phone number form, or allows emtpy strings + /// + /// + /// + /// + public static IRuleBuilderOptions EmptyPhoneNumber(this IRuleBuilder builder) + { + return builder.Must(static phone => !(phone?.Length).HasValue || PhoneRegex.IsMatch(phone)) + .WithMessage("{PropertyValue} is not a valid phone number."); + } + + /// + /// Checks a string against . + /// If the string is null or empty, it is allowed. + /// + /// + /// + /// + public static IRuleBuilderOptions SpecialCharacters(this IRuleBuilder builder) + { + return builder.Must(static str => str == null || !SpecialCharactersRegx.IsMatch(str)) + .WithMessage("{PropertyName} contains illegal characters"); + } + /// + /// Checks a string against . + /// If the string is null or empty, it is allowed. + /// + /// + /// + /// + public static IRuleBuilderOptions IllegalCharacters(this IRuleBuilder builder) + { + return builder.Must(static str => str == null || !IllegalRegx.IsMatch(str)) + .WithMessage("{PropertyName} contains illegal characters"); + } + /// + /// Makes sure a field contains at least 1 character a-Z + /// + /// + /// + /// + public static IRuleBuilderOptions Alpha(this IRuleBuilder builder) + { + return builder.Must(static str => str == null || AlphaRegx.IsMatch(str)) + .WithMessage("{PropertyName} requires at least one a-Z character."); + } + /// + /// Determines if all characters are only a-Z (allows whitespace) + /// + /// + /// + /// + public static IRuleBuilderOptions AlphaOnly(this IRuleBuilder builder) + { + return builder.Must(static str => str == null || OnlyAlphaRegx.IsMatch(str)) + .WithMessage("{PropertyName} can only be a alpha character from a-Z."); + } + /// + /// Makes sure a field contains at least 1 numeral + /// + /// + /// + /// + public static IRuleBuilderOptions Numeric(this IRuleBuilder builder) + { + return builder.Must(static str => str == null || NumericRegx.IsMatch(str)) + .WithMessage("{PropertyName} requires at least one number."); + } + /// + /// Determines if all characters are only 0-9 (not whitespace is allowed) + /// + /// + /// + /// + public static IRuleBuilderOptions NumericOnly(this IRuleBuilder builder) + { + return builder.Must(static str => str == null || OnlyNumericRegx.IsMatch(str)) + .WithMessage("{PropertyName} can only be a number 0-9."); + } + /// + /// Makes sure the field contains at least 1 alpha numeric character (whitespace included) + /// + /// + /// + /// + public static IRuleBuilderOptions AlphaNumeric(this IRuleBuilder builder) + { + return builder.Must(static str => str == null || AlphaNumRegx.IsMatch(str)) + .WithMessage("{PropertyName} must contain at least one alpha-numeric character."); + } + /// + /// Determines if all characters are only alpha-numeric (whitespace allowed) + /// + /// + /// + /// + public static IRuleBuilderOptions AlphaNumericOnly(this IRuleBuilder builder) + { + return builder.Must(static str => str == null || OnlyAlphaNumRegx.IsMatch(str)) + .WithMessage("{PropertyName} can only contain alpha numeric characters."); + } + /// + /// Tests the string against the password regular expression to determine if the + /// value meets the basic password requirements + /// + /// + /// + /// + public static IRuleBuilderOptions Password(this IRuleBuilder builder) + { + return builder.Must(static str => str == null || PasswordRegx.IsMatch(str)) + .WithMessage("{PropertyName} does not meet password requirements."); + } + /// + /// Defines a length validator on the current rule builder, but only for string properties. + /// Validation will fail if the length of the string is outside of the specified range. + /// The range is inclusive + /// + /// + /// + /// The length range of the specified string + /// + public static IRuleBuilderOptions Length(this IRuleBuilder builder, Range lengthRange) + { + return builder.Length(lengthRange.Start.Value, lengthRange.End.Value); + } + } +} -- cgit