diff options
Diffstat (limited to 'VNLib.Plugins.Extensions.Validation')
6 files changed, 0 insertions, 533 deletions
diff --git a/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.csproj b/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.csproj deleted file mode 100644 index ef10ecf..0000000 --- a/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.csproj +++ /dev/null @@ -1,36 +0,0 @@ -<Project Sdk="Microsoft.NET.Sdk"> - - <PropertyGroup> - <TargetFramework>net6.0</TargetFramework> - <Authors>Vaughn Nugent</Authors> - <Copyright>Copyright © 2022 Vaughn Nugent</Copyright> - <PackageProjectUrl>https://www.vaughnnugent.com/resources</PackageProjectUrl> - <Version>1.0.1.1</Version> - <SignAssembly>True</SignAssembly> - <AssemblyOriginatorKeyFile>\\vaughnnugent.com\Internal\Folder Redirection\vman\Documents\Programming\Software\StrongNameingKey.snk</AssemblyOriginatorKeyFile> - </PropertyGroup> - - <PropertyGroup> - <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> - <GenerateDocumentationFile>True</GenerateDocumentationFile> - <AnalysisLevel>latest-all</AnalysisLevel> - <Nullable>enable</Nullable> - </PropertyGroup> - - <ItemGroup> - <PackageReference Include="ErrorProne.NET.CoreAnalyzers" Version="0.1.2"> - <PrivateAssets>all</PrivateAssets> - <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> - </PackageReference> - <PackageReference Include="ErrorProne.NET.Structs" Version="0.1.2"> - <PrivateAssets>all</PrivateAssets> - <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> - </PackageReference> - <PackageReference Include="FluentValidation" Version="11.3.0" /> - </ItemGroup> - - <ItemGroup> - <ProjectReference Include="..\..\..\VNLib\Plugins\src\VNLib.Plugins.csproj" /> - </ItemGroup> - -</Project> diff --git a/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.xml b/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.xml deleted file mode 100644 index a4d1217..0000000 --- a/VNLib.Plugins.Extensions.Validation/VNLib.Plugins.Extensions.Validation.xml +++ /dev/null @@ -1,137 +0,0 @@ -<?xml version="1.0"?> -<!-- -Copyright (c) 2022 Vaughn Nugent ---> -<doc> - <assembly> - <name>VNLib.Plugins.Extensions.Validation</name> - </assembly> - <members> - <member name="T:VNLib.Plugins.Extensions.Validation.ValErrWebMessage"> - <summary> - Extends the <see cref="T:VNLib.Plugins.WebMessage"/> class with provisions for a collection of validations - </summary> - </member> - <member name="P:VNLib.Plugins.Extensions.Validation.ValErrWebMessage.Errors"> - <summary> - A collection of error messages to send to clients - </summary> - </member> - <member name="T:VNLib.Plugins.Extensions.Validation.ValidationExtensions"> - <summary> - Defines extenstion methods for <see cref="T:FluentValidation.IRuleBuilder`2"/> - </summary> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.GetErrorsAsCollection(FluentValidation.Results.ValidationResult)"> - <summary> - Gets a collection of Json-serializable validation errors - </summary> - <param name="result"></param> - <returns>A collection of json errors to return to a user</returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.PhoneNumber``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Tests the the property against <see cref="F:VNLib.Plugins.Extensions.Validation.ValidationExtensions.PhoneRegex"/> - to determine if the string matches the proper phone number form - </summary> - <typeparam name="T"></typeparam> - <param name="validator"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.EmptyPhoneNumber``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Tests the the property against <see cref="F:VNLib.Plugins.Extensions.Validation.ValidationExtensions.PhoneRegex"/> - to determine if the string matches the proper phone number form, or allows emtpy strings - </summary> - <typeparam name="T"></typeparam> - <param name="validator"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.SpecialCharacters``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Checks a string against <see cref="!:Statics.SpecialCharacters"/>. - If the string is null or empty, it is allowed. - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.IllegalCharacters``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Checks a string against <see cref="!:Statics.IllegalChars"/>. - If the string is null or empty, it is allowed. - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.Alpha``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Makes sure a field contains at least 1 character a-Z - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.AlphaOnly``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Determines if all characters are only a-Z (allows whitespace) - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.Numeric``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Makes sure a field contains at least 1 numeral - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.NumericOnly``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Determines if all characters are only 0-9 (not whitespace is allowed) - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.AlphaNumeric``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Makes sure the field contains at least 1 alpha numeric character (whitespace included) - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.AlphaNumericOnly``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Determines if all characters are only alpha-numeric (whitespace allowed) - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.Password``1(FluentValidation.IRuleBuilder{``0,System.String})"> - <summary> - Tests the string against the password regular expression to determine if the - value meets the basic password requirements - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <returns></returns> - </member> - <member name="M:VNLib.Plugins.Extensions.Validation.ValidationExtensions.Length``1(FluentValidation.IRuleBuilder{``0,System.String},System.Range)"> - <summary> - 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 - </summary> - <typeparam name="T"></typeparam> - <param name="builder"></param> - <param name="lengthRange">The length range of the specified string</param> - <returns></returns> - </member> - </members> -</doc> diff --git a/VNLib.Plugins.Extensions.Validation/ValErrWebMessage.cs b/VNLib.Plugins.Extensions.Validation/ValErrWebMessage.cs deleted file mode 100644 index 8e439da..0000000 --- a/VNLib.Plugins.Extensions.Validation/ValErrWebMessage.cs +++ /dev/null @@ -1,41 +0,0 @@ -/* -* Copyright (c) 2022 Vaughn Nugent -* -* Library: VNLib -* Package: VNLib.Plugins.Extensions.Validation -* File: ValErrWebMessage.cs -* -* ValErrWebMessage.cs is part of VNLib.Plugins.Extensions.Validation which is part of the larger -* VNLib collection of libraries and utilities. -* -* VNLib.Plugins.Extensions.Validation 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 2 of the License, -* or (at your option) any later version. -* -* VNLib.Plugins.Extensions.Validation 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 VNLib.Plugins.Extensions.Validation. If not, see http://www.gnu.org/licenses/. -*/ - -using System.Collections; -using System.Text.Json.Serialization; - -namespace VNLib.Plugins.Extensions.Validation -{ - /// <summary> - /// Extends the <see cref="WebMessage"/> class with provisions for a collection of validations - /// </summary> - public class ValErrWebMessage : WebMessage - { - /// <summary> - /// A collection of error messages to send to clients - /// </summary> - [JsonPropertyName("errors")] - public ICollection Errors { get; set; } - } -} diff --git a/VNLib.Plugins.Extensions.Validation/ValidationErrorMessage.cs b/VNLib.Plugins.Extensions.Validation/ValidationErrorMessage.cs deleted file mode 100644 index 63a1d63..0000000 --- a/VNLib.Plugins.Extensions.Validation/ValidationErrorMessage.cs +++ /dev/null @@ -1,36 +0,0 @@ -/* -* Copyright (c) 2022 Vaughn Nugent -* -* Library: VNLib -* Package: VNLib.Plugins.Extensions.Validation -* File: ValidationErrorMessage.cs -* -* ValidationErrorMessage.cs is part of VNLib.Plugins.Extensions.Validation which is part of the larger -* VNLib collection of libraries and utilities. -* -* VNLib.Plugins.Extensions.Validation 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 2 of the License, -* or (at your option) any later version. -* -* VNLib.Plugins.Extensions.Validation 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 VNLib.Plugins.Extensions.Validation. If not, see http://www.gnu.org/licenses/. -*/ - -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 deleted file mode 100644 index 7ca45e9..0000000 --- a/VNLib.Plugins.Extensions.Validation/ValidationExtensions.cs +++ /dev/null @@ -1,78 +0,0 @@ -/* -* Copyright (c) 2022 Vaughn Nugent -* -* Library: VNLib -* Package: VNLib.Plugins.Extensions.Validation -* File: ValidationExtensions.cs -* -* ValidationExtensions.cs is part of VNLib.Plugins.Extensions.Validation which is part of the larger -* VNLib collection of libraries and utilities. -* -* VNLib.Plugins.Extensions.Validation 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 2 of the License, -* or (at your option) any later version. -* -* VNLib.Plugins.Extensions.Validation 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 VNLib.Plugins.Extensions.Validation. If not, see http://www.gnu.org/licenses/. -*/ - -using System; -using System.Diagnostics.CodeAnalysis; - -using FluentValidation; -using FluentValidation.Results; - -#nullable enable - -namespace VNLib.Plugins.Extensions.Validation -{ - /// <summary> - /// Provides shortcut methods to aid programmatic validation of objects - /// </summary> - public static class ValidationExtensions - { - /// <summary> - /// If <paramref name="assertion"/> evalues to false, sets the specified assertion message - /// to the <see cref="WebMessage.Result"/> to the specified string - /// </summary> - /// <param name="webm"></param> - /// <param name="assertion">The result of the assertion</param> - /// <param name="message">The error message to store when the value is false</param> - /// <returns>The inverse of <paramref name="assertion"/></returns> - public static bool Assert(this WebMessage webm, [DoesNotReturnIf(false)] bool assertion, string message) - { - if(!assertion) - { - webm.Success = false; - webm.Result = message; - } - return !assertion; - } - /// <summary> - /// Validates the specified instance, and stores errors to the specified <paramref name="webMessage"/> - /// and sets the <see cref="ValErrWebMessage.IsError"/> - /// </summary> - /// <param name="instance">The instance to validate</param> - /// <param name="validator"></param> - /// <param name="webMessage">The <see cref="ValErrWebMessage"/> to store errors to</param> - /// <returns>True if the result of the validation is valid, false otherwise and the <paramref name="webMessage"/> is not modified</returns> - public static bool Validate<T>(this IValidator<T> 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 deleted file mode 100644 index 63b4f07..0000000 --- a/VNLib.Plugins.Extensions.Validation/ValidatorExtensions.cs +++ /dev/null @@ -1,205 +0,0 @@ -/* -* Copyright (c) 2022 Vaughn Nugent -* -* Library: VNLib -* Package: VNLib.Plugins.Extensions.Validation -* File: ValidatorExtensions.cs -* -* ValidatorExtensions.cs is part of VNLib.Plugins.Extensions.Validation which is part of the larger -* VNLib collection of libraries and utilities. -* -* VNLib.Plugins.Extensions.Validation 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 2 of the License, -* or (at your option) any later version. -* -* VNLib.Plugins.Extensions.Validation 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 VNLib.Plugins.Extensions.Validation. If not, see http://www.gnu.org/licenses/. -*/ - -using System; -using System.Collections; -using System.Text.RegularExpressions; - -using FluentValidation; -using FluentValidation.Results; - -namespace VNLib.Plugins.Extensions.Validation -{ - /// <summary> - /// Defines extenstion methods for <see cref="IRuleBuilder{T, TProperty}"/> - /// </summary> - 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); - - - /// <summary> - /// Gets a collection of Json-serializable validation errors - /// </summary> - /// <param name="result"></param> - /// <returns>A collection of json errors to return to a user</returns> - public static ICollection GetErrorsAsCollection(this ValidationResult result) - { - return result.Errors.ConvertAll(static err => new ValidationErrorMessage { ErrorMessage = err.ErrorMessage, PropertyName = err.PropertyName }); - } - - /// <summary> - /// Tests the the property against <see cref="PhoneRegex"/> - /// to determine if the string matches the proper phone number form - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> PhoneNumber<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static phone => phone?.Length > 0 && PhoneRegex.IsMatch(phone)) - .WithMessage("{PropertyValue} is not a valid phone number."); - } - /// <summary> - /// Tests the the property against <see cref="PhoneRegex"/> - /// to determine if the string matches the proper phone number form, or allows emtpy strings - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> EmptyPhoneNumber<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static phone => phone == null || phone.Length == 0 || PhoneRegex.IsMatch(phone)) - .WithMessage("{PropertyValue} is not a valid phone number."); - } - - /// <summary> - /// Checks a string against <see cref="SpecialCharactersRegx"/>. - /// If the string is null or empty, it is allowed. - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> SpecialCharacters<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static str => str == null || !SpecialCharactersRegx.IsMatch(str)) - .WithMessage("{PropertyName} contains illegal characters"); - } - /// <summary> - /// Checks a string against <see cref="Statics.IllegalChars"/>. - /// If the string is null or empty, it is allowed. - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> IllegalCharacters<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static str => str == null || !IllegalRegx.IsMatch(str)) - .WithMessage("{PropertyName} contains illegal characters"); - } - /// <summary> - /// Makes sure a field contains at least 1 character a-Z - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> Alpha<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static str => str == null || AlphaRegx.IsMatch(str)) - .WithMessage("{PropertyName} requires at least one a-Z character."); - } - /// <summary> - /// Determines if all characters are only a-Z (allows whitespace) - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> AlphaOnly<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static str => str == null || OnlyAlphaRegx.IsMatch(str)) - .WithMessage("{PropertyName} can only be a alpha character from a-Z."); - } - /// <summary> - /// Makes sure a field contains at least 1 numeral - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> Numeric<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static str => str == null || NumericRegx.IsMatch(str)) - .WithMessage("{PropertyName} requires at least one number."); - } - /// <summary> - /// Determines if all characters are only 0-9 (not whitespace is allowed) - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> NumericOnly<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static str => str == null || OnlyNumericRegx.IsMatch(str)) - .WithMessage("{PropertyName} can only be a number 0-9."); - } - /// <summary> - /// Makes sure the field contains at least 1 alpha numeric character (whitespace included) - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> AlphaNumeric<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static str => str == null || AlphaNumRegx.IsMatch(str)) - .WithMessage("{PropertyName} must contain at least one alpha-numeric character."); - } - /// <summary> - /// Determines if all characters are only alpha-numeric (whitespace allowed) - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> AlphaNumericOnly<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static str => str == null || OnlyAlphaNumRegx.IsMatch(str)) - .WithMessage("{PropertyName} can only contain alpha numeric characters."); - } - /// <summary> - /// Tests the string against the password regular expression to determine if the - /// value meets the basic password requirements - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> Password<T>(this IRuleBuilder<T, string> builder) - { - return builder.Must(static str => str == null || PasswordRegx.IsMatch(str)) - .WithMessage("{PropertyName} does not meet password requirements."); - } - /// <summary> - /// 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 - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="builder"></param> - /// <param name="lengthRange">The length range of the specified string</param> - /// <returns></returns> - public static IRuleBuilderOptions<T, string> Length<T>(this IRuleBuilder<T, string> builder, Range lengthRange) - { - return builder.Length(lengthRange.Start.Value, lengthRange.End.Value); - } - } -} |