├── .editorconfig
├── .github
└── workflows
│ ├── build.yml
│ └── publish.yml
├── .gitignore
├── Directory.Build.Props
├── LICENSE.md
├── README.md
├── Throw.sln
├── assets
├── getting-started.png
├── icon-square.png
└── icon.png
├── src
├── Common
│ ├── ExceptionCustomizations.cs
│ ├── ExceptionThrower.cs
│ └── Validatable.cs
├── Properties
│ └── AssemblyInfo.cs
├── Throw.csproj
├── ValidatableCreation
│ ├── ValidatableCreationExtensions.Nullables.cs
│ └── ValidatableCreationExtensions.cs
├── ValidatableExtensions
│ ├── StringProperties
│ │ ├── ValidatableExtensions.StringProperties.Equality.cs
│ │ ├── ValidatableExtensions.StringProperties.Length.cs
│ │ ├── ValidatableExtensions.StringProperties.Regex.cs
│ │ └── ValidatableExtensions.StringProperties.Substring.cs
│ ├── Strings
│ │ ├── ValidatableExtensions.Strings.Equality.cs
│ │ ├── ValidatableExtensions.Strings.Length.cs
│ │ ├── ValidatableExtensions.Strings.Regex.cs
│ │ └── ValidatableExtensions.Strings.Substring.cs
│ ├── ValidatableExtensions.BooleanProperties.cs
│ ├── ValidatableExtensions.Booleans.cs
│ ├── ValidatableExtensions.CollectionProperties.cs
│ ├── ValidatableExtensions.Collections.cs
│ ├── ValidatableExtensions.ComparableProperties.cs
│ ├── ValidatableExtensions.Comparables.cs
│ ├── ValidatableExtensions.ConditionalCompilation.cs
│ ├── ValidatableExtensions.DateTimeProperties.cs
│ ├── ValidatableExtensions.DateTimes.cs
│ ├── ValidatableExtensions.EnumProperties.cs
│ ├── ValidatableExtensions.Enums.cs
│ ├── ValidatableExtensions.Equalities.cs
│ ├── ValidatableExtensions.EqualityProperties.cs
│ ├── ValidatableExtensions.UriProperties.cs
│ └── ValidatalbeExtensions.Uris.cs
├── ValidatableMethods
│ └── ValidatableMethods.Types.cs
├── Validators
│ ├── Validator.Collections.cs
│ ├── Validator.Comperables.cs
│ ├── Validator.DateTimes.cs
│ ├── Validator.Enums.cs
│ ├── Validator.Equalities.cs
│ ├── Validator.Strings.cs
│ ├── Validator.Types.cs
│ └── Validator.Uris.cs
├── _usings.cs
└── stylecop.json
└── tests
└── UnitTests
├── .editorconfig
├── Common
├── ExceptionThrowerTests.cs
└── ValidatableTests.cs
├── Throw.UnitTests.csproj
├── Utils
└── ParameterConstants.cs
├── ValidatableCreation
├── ValidatableCreationExtensions.NullablesTests.cs
└── ValidatableCreationExtensionsTests.cs
├── ValidatableExtensions
├── StringProperties
│ ├── ValidatableExtensions.StringProperties.EqualityTests.cs
│ ├── ValidatableExtensions.StringProperties.LengthTests.cs
│ ├── ValidatableExtensions.StringProperties.RegexTests.cs
│ └── ValidatableExtensions.StringProperties.SubstringTests.cs
├── Strings
│ ├── ValidatableExtensions.Strings.EqualityTests.cs
│ ├── ValidatableExtensions.Strings.LengthTests.cs
│ ├── ValidatableExtensions.Strings.RegexTests.cs
│ └── ValidatableExtensions.Strings.SubstringTests.cs
├── ValidatableExtensions.BooleanPropertiesTests.cs
├── ValidatableExtensions.BooleansTests.cs
├── ValidatableExtensions.CollectionPropertiesTests.cs
├── ValidatableExtensions.CollectionsTests.cs
├── ValidatableExtensions.ComparablePropertiesTests.cs
├── ValidatableExtensions.ComparablesTests.cs
├── ValidatableExtensions.ConditionalCompilationTests.cs
├── ValidatableExtensions.DateTimePropertiesTests.cs
├── ValidatableExtensions.DateTimesTests.cs
├── ValidatableExtensions.EnumPropertiesTests.cs
├── ValidatableExtensions.EnumsTests.cs
├── ValidatableExtensions.EqualitiesTests.cs
├── ValidatableExtensions.EqualityPropertiesTests.cs
├── ValidatableExtensions.UriPropertiesTests.cs
└── ValidatableExtensions.UrisTests.cs
├── ValidatableMethods
└── ValidatableMethods.TypesTests.cs
├── Validators
└── Validator.CollectionsTests.cs
└── _usings.cs
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = space
5 | indent_size = 4
6 | end_of_line = crlf
7 | charset = utf-8
8 | trim_trailing_whitespace = true
9 | insert_final_newline = true
10 |
11 | # SA1615: Element return value should be documented.
12 | dotnet_diagnostic.SA1615.severity = none
13 |
14 | # SA1611: Element parameters must be documented.
15 | dotnet_diagnostic.SA1611.severity = none
16 |
17 | # SA1633: File must have header.
18 | dotnet_diagnostic.SA1633.severity = none
19 |
20 | # SA1633: Generic type parameters must be documented.
21 | dotnet_diagnostic.SA1618.severity = none
22 |
23 | # SA1133: Each attribute should be placed in its own set of square brackets.
24 | dotnet_diagnostic.SA1133.severity = none
25 |
26 | # SA1600: Elements must be documented.
27 | dotnet_diagnostic.SA1600.severity = none
28 |
29 | # SA1601: Partial elementes should be documented.
30 | dotnet_diagnostic.SA1601.severity = none
31 |
32 | # SA1313: Parameter names must begin with lower case letter.
33 | dotnet_diagnostic.SA1313.severity = none
34 |
35 | # SA1009: Closing parenthesis should be followed by a space.
36 | dotnet_diagnostic.SA1009.severity = none
37 |
38 | # SA1000: The keyword 'new' should be followed by a space.
39 | dotnet_diagnostic.SA1000.severity = none
40 |
41 | # IDE0008: Use explicit type
42 | csharp_style_var_when_type_is_apparent = true
43 |
44 | # IDE0130: Namespace does not match folder structure
45 | dotnet_style_namespace_match_folder = false
46 |
47 | # IDE0023: Use block body for operators
48 | csharp_style_expression_bodied_operators = when_on_single_line
49 |
50 | # IDE0130: Namespace does not match folder structure
51 | dotnet_diagnostic.IDE0130.severity = none
52 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | jobs:
10 | build:
11 |
12 | runs-on: windows-latest
13 |
14 | steps:
15 | - uses: actions/checkout@v2
16 | - name: Setup .NET
17 | uses: actions/setup-dotnet@v1
18 | with:
19 | dotnet-version: 6.0.x
20 | - name: Restore dependencies
21 | run: dotnet restore
22 | - name: Build
23 | run: dotnet build -c Release --no-restore
24 | - name: Test Debug
25 | run: dotnet test -c Debug --no-restore --verbosity normal --collect:"XPlat Code Coverage"
26 | - name: Test Release
27 | run: dotnet test -c Release --no-restore --verbosity normal --collect:"XPlat Code Coverage"
28 | - name: Upload coverage to Codecov
29 | uses: codecov/codecov-action@v1
30 | with:
31 | token: ${{ secrets.CODE_COV_TOKEN }}
32 |
--------------------------------------------------------------------------------
/.github/workflows/publish.yml:
--------------------------------------------------------------------------------
1 | name: publish Throw to nuget
2 | on:
3 | workflow_dispatch:
4 | push:
5 | branches:
6 | - main # Default release branch
7 | paths:
8 | - 'src/**'
9 | jobs:
10 | publish:
11 | name: build, pack & publish
12 | runs-on: windows-latest
13 | steps:
14 | - uses: actions/checkout@v2
15 |
16 | - name: Setup dotnet
17 | uses: actions/setup-dotnet@v1
18 | with:
19 | dotnet-version: 6.0.x
20 |
21 | # Publish
22 | - name: Package
23 | run: dotnet pack -c Release src\Throw.csproj
24 | - name: Publish
25 | run: dotnet nuget push artifacts\*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
26 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | **/.ionide
2 | **/.vscode
3 | **/.vs
4 | **/.idea
5 | **/obj
6 | **/bin
7 | **/artifacts
8 | **/TestResults
9 |
10 | .fake
11 | .ionide
12 |
13 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
14 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
15 |
16 | # User-specific stuff
17 | .idea/**/workspace.xml
18 | .idea/**/tasks.xml
19 | .idea/**/usage.statistics.xml
20 | .idea/**/dictionaries
21 | .idea/**/shelf
22 |
23 | # AWS User-specific
24 | .idea/**/aws.xml
25 |
26 | # Generated files
27 | .idea/**/contentModel.xml
28 |
29 | # Sensitive or high-churn files
30 | .idea/**/dataSources/
31 | .idea/**/dataSources.ids
32 | .idea/**/dataSources.local.xml
33 | .idea/**/sqlDataSources.xml
34 | .idea/**/dynamic.xml
35 | .idea/**/uiDesigner.xml
36 | .idea/**/dbnavigator.xml
37 |
38 | # Gradle
39 | .idea/**/gradle.xml
40 | .idea/**/libraries
41 |
42 | # Gradle and Maven with auto-import
43 | # When using Gradle or Maven with auto-import, you should exclude module files,
44 | # since they will be recreated, and may cause churn. Uncomment if using
45 | # auto-import.
46 | # .idea/artifacts
47 | # .idea/compiler.xml
48 | # .idea/jarRepositories.xml
49 | # .idea/modules.xml
50 | # .idea/*.iml
51 | # .idea/modules
52 | # *.iml
53 | # *.ipr
54 |
55 | # CMake
56 | cmake-build-*/
57 |
58 | # Mongo Explorer plugin
59 | .idea/**/mongoSettings.xml
60 |
61 | # File-based project format
62 | *.iws
63 |
64 | # IntelliJ
65 | out/
66 |
67 | # mpeltonen/sbt-idea plugin
68 | .idea_modules/
69 |
70 | # JIRA plugin
71 | atlassian-ide-plugin.xml
72 |
73 | # Cursive Clojure plugin
74 | .idea/replstate.xml
75 |
76 | # SonarLint plugin
77 | .idea/sonarlint/
78 |
79 | # Crashlytics plugin (for Android Studio and IntelliJ)
80 | com_crashlytics_export_strings.xml
81 | crashlytics.properties
82 | crashlytics-build.properties
83 | fabric.properties
84 |
85 | # Editor-based Rest Client
86 | .idea/httpRequests
87 |
88 | # Android studio 3.1+ serialized cache file
89 | .idea/caches/build_file_checksums.ser
90 |
--------------------------------------------------------------------------------
/Directory.Build.Props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | true
7 | net6.0
8 | enable
9 | enable
10 | True
11 | true
12 |
13 |
14 |
15 |
16 | all
17 | runtime; build; native; contentfiles; analyzers
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Amichai Mantinband
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Throw.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.3.32519.111
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Throw", "src\Throw.csproj", "{EFF12011-026D-48E3-9EEE-5F1B5C2D0FFC}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0289805B-A7C8-441D-BD46-C05D135FAD23}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Throw.UnitTests", "tests\UnitTests\Throw.UnitTests.csproj", "{FB6525E8-E040-4F68-9152-548C6D64EEAB}"
11 | EndProject
12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".sln", ".sln", "{78F97BB4-8243-479B-81C4-887AB3B173B5}"
13 | ProjectSection(SolutionItems) = preProject
14 | .editorconfig = .editorconfig
15 | .gitignore = .gitignore
16 | Directory.Build.Props = Directory.Build.Props
17 | LICENSE.md = LICENSE.md
18 | README.md = README.md
19 | EndProjectSection
20 | EndProject
21 | Global
22 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
23 | Debug|Any CPU = Debug|Any CPU
24 | Release|Any CPU = Release|Any CPU
25 | EndGlobalSection
26 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
27 | {EFF12011-026D-48E3-9EEE-5F1B5C2D0FFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28 | {EFF12011-026D-48E3-9EEE-5F1B5C2D0FFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
29 | {EFF12011-026D-48E3-9EEE-5F1B5C2D0FFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
30 | {EFF12011-026D-48E3-9EEE-5F1B5C2D0FFC}.Release|Any CPU.Build.0 = Release|Any CPU
31 | {FB6525E8-E040-4F68-9152-548C6D64EEAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32 | {FB6525E8-E040-4F68-9152-548C6D64EEAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
33 | {FB6525E8-E040-4F68-9152-548C6D64EEAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
34 | {FB6525E8-E040-4F68-9152-548C6D64EEAB}.Release|Any CPU.Build.0 = Release|Any CPU
35 | EndGlobalSection
36 | GlobalSection(SolutionProperties) = preSolution
37 | HideSolutionNode = FALSE
38 | EndGlobalSection
39 | GlobalSection(NestedProjects) = preSolution
40 | {FB6525E8-E040-4F68-9152-548C6D64EEAB} = {0289805B-A7C8-441D-BD46-C05D135FAD23}
41 | EndGlobalSection
42 | GlobalSection(ExtensibilityGlobals) = postSolution
43 | SolutionGuid = {3FBC4496-672C-438E-923F-79A855956FEC}
44 | EndGlobalSection
45 | EndGlobal
46 |
--------------------------------------------------------------------------------
/assets/getting-started.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/amantinband/throw/b559d6f99a233e193ab21e0b01d8841a61ae4a22/assets/getting-started.png
--------------------------------------------------------------------------------
/assets/icon-square.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/amantinband/throw/b559d6f99a233e193ab21e0b01d8841a61ae4a22/assets/icon-square.png
--------------------------------------------------------------------------------
/assets/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/amantinband/throw/b559d6f99a233e193ab21e0b01d8841a61ae4a22/assets/icon.png
--------------------------------------------------------------------------------
/src/Common/ExceptionCustomizations.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// The exception customizations.
5 | /// Contains a discriminated union of all possible exception customization options.
6 | ///
7 | public struct ExceptionCustomizations
8 | {
9 | ///
10 | /// Initializes a new instance of the struct.
11 | ///
12 | public ExceptionCustomizations(OneOf, Func> customization)
13 | {
14 | this.Customization = customization;
15 | }
16 |
17 | ///
18 | /// Gets a discriminated union of all possible exception customization options.
19 | ///
20 | public OneOf, Func> Customization { get; }
21 |
22 | ///
23 | /// Initializes a new instance of the class.
24 | /// The customization will be the given .
25 | ///
26 | public static implicit operator ExceptionCustomizations(string message) => new(message);
27 |
28 | ///
29 | /// Initializes a new instance of the class.
30 | /// The customization will be an exception of the given .
31 | ///
32 | public static implicit operator ExceptionCustomizations(Type type) => new(type);
33 |
34 | ///
35 | /// Initializes a new instance of the class.
36 | /// The customization will be the given exception returning .
37 | ///
38 | public static implicit operator ExceptionCustomizations(Func func) => new(func);
39 |
40 | ///
41 | /// Initializes a new instance of the class.
42 | /// The customization will be the given exception returning .
43 | ///
44 | public static implicit operator ExceptionCustomizations(Func func) => new(func);
45 |
46 | ///
47 | /// Initializes a new instance of the class.
48 | /// The customization will match the given .
49 | ///
50 | public static implicit operator ExceptionCustomizations(OneOf, Func> customizations) => new(customizations);
51 | }
--------------------------------------------------------------------------------
/src/Common/ExceptionThrower.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Exception throwing extensions.
5 | ///
6 | public static class ExceptionThrower
7 | {
8 | ///
9 | /// Throws an , unless the defines a custom exception.
10 | ///
11 | [DoesNotReturn, MethodImpl(MethodImplOptions.AggressiveInlining)]
12 | public static void ThrowNull(
13 | string paramName,
14 | ExceptionCustomizations? exceptionCustomizations = null,
15 | string? generalMessage = "Value cannot be null.")
16 | {
17 | if (exceptionCustomizations is null)
18 | {
19 | throw new ArgumentNullException(paramName: paramName, message: generalMessage);
20 | }
21 |
22 | throw exceptionCustomizations.Value.Customization.Match(
23 | message => new ArgumentNullException(paramName: paramName, message: message ?? generalMessage),
24 | type => (Exception)Activator.CreateInstance(type)!,
25 | func => func(),
26 | func => func(paramName));
27 | }
28 |
29 | ///
30 | /// Throws an , unless the defines a custom exception.
31 | ///
32 | [DoesNotReturn, MethodImpl(MethodImplOptions.AggressiveInlining)]
33 | public static void ThrowOutOfRange(
34 | string paramName,
35 | TValue actualValue,
36 | ExceptionCustomizations? exceptionCustomizations = null,
37 | string? generalMessage = "Specified argument was out of the range of valid values.")
38 | {
39 | if (exceptionCustomizations is null)
40 | {
41 | throw new ArgumentOutOfRangeException(paramName: paramName, actualValue, message: generalMessage);
42 | }
43 |
44 | throw exceptionCustomizations.Value.Customization.Match(
45 | message => new ArgumentOutOfRangeException(paramName: paramName, actualValue, message: message ?? generalMessage),
46 | type => (Exception)Activator.CreateInstance(type)!,
47 | func => func(),
48 | func => func(paramName));
49 | }
50 |
51 | ///
52 | /// Throws an , unless the defines a custom exception.
53 | ///
54 | [DoesNotReturn, MethodImpl(MethodImplOptions.AggressiveInlining)]
55 | public static void Throw(
56 | string paramName,
57 | ExceptionCustomizations? exceptionCustomizations = null,
58 | string? generalMessage = null)
59 | {
60 | if (exceptionCustomizations is null)
61 | {
62 | throw new ArgumentException(message: generalMessage, paramName: paramName);
63 | }
64 |
65 | throw exceptionCustomizations.Value.Customization.Match(
66 | message => new ArgumentException(message: message ?? generalMessage, paramName: paramName),
67 | type => (Exception)Activator.CreateInstance(type)!,
68 | func => func(),
69 | func => func(paramName));
70 | }
71 | }
--------------------------------------------------------------------------------
/src/Common/Validatable.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Creates a new instance with the specified value.
5 | /// The instance can be used to throw exceptions if the value matches a condition specified.
6 | ///
7 | /// The value to be validated.
8 | /// The name of the parameter holding the .
9 | /// Customizations to the exception, which will be applied if an exception is thrown.
10 | [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this", Justification = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3201: SA1101 shows on 'with' expressions")]
11 | public readonly partial record struct Validatable(TValue Value, string ParamName, ExceptionCustomizations? ExceptionCustomizations = null)
12 | where TValue : notnull
13 | {
14 | ///
15 | /// Implicit conversion operator back to the original value's type.
16 | ///
17 | public static implicit operator TValue([DisallowNull] Validatable validatable) => validatable.Value;
18 |
19 | ///
20 | /// Creates a validatable with the specified exception customization.
21 | ///
22 | /// A custom exception message which will be used instead of the default message.
23 | public Validatable Throw([DisallowNull] string message) =>
24 | this with { ExceptionCustomizations = message };
25 |
26 | ///
27 | /// Creates a validatable with the specified exception customization.
28 | ///
29 | ///
30 | /// A function which returns an excpetion. This function will be used to create the exception that will
31 | /// be thrown if a condition is matched.
32 | ///
33 | public Validatable Throw([DisallowNull] Func exceptionThrower) =>
34 | this with { ExceptionCustomizations = exceptionThrower };
35 |
36 | ///
37 | /// Creates a validatable with the specified exception customization.
38 | ///
39 | ///
40 | /// A function which receives the parameter name returns an excpetion.
41 | /// This function will be used to create the exception that will be thrown if a condition is matched.
42 | /// For example: paramName => throw new Exception($"Parameter name: {paramName}").
43 | ///
44 | public Validatable Throw([DisallowNull] Func exceptionThrower) =>
45 | this with { ExceptionCustomizations = exceptionThrower };
46 |
47 | ///
48 | /// Creates a validatable with the specified exception customization.
49 | ///
50 | /// The type of the exception to be thrown.
51 | public Validatable Throw()
52 | where TException : notnull, Exception, new() => this with { ExceptionCustomizations = typeof(TException) };
53 |
54 | ///
55 | /// Creates a validatable with the no customizations.
56 | ///
57 | public Validatable Throw() => this with { ExceptionCustomizations = null };
58 | }
59 |
--------------------------------------------------------------------------------
/src/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | [assembly: InternalsVisibleTo("Throw.UnitTests")]
2 |
--------------------------------------------------------------------------------
/src/Throw.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Throw
5 | 1.4.0
6 | Amichai Mantinband
7 | icon-square.png
8 | argument,guard,clause,exception,contract,assert,assertions,validation
9 | A simple, fluent, extensible, and fully customizable library for throwing exceptions using .NET 6+
10 | https://github.com/amantinband/throw
11 | git
12 | https://opensource.org/licenses/MIT
13 | https://github.com/amantinband/throw
14 | ../artifacts/
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/src/ValidatableCreation/ValidatableCreationExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extensions for creating s.
5 | ///
6 | public static partial class ValidatableCreationExtensions
7 | {
8 | ///
9 | /// Creates a new instance with the specified value.
10 | /// The instance can be used to throw exceptions if the value matches a condition specified.
11 | ///
12 | /// The value to be validated.
13 | /// A function which returns an excpetion. This function will be used to create the exception that will be thrown if a condition is matched.
14 | /// Doesn't need to be specified. Will be populated by the compiler.
15 | ///
16 | /// This extension method is intended for non-nullable types.
17 | /// For nullable types, use the extension method.
18 | ///
19 | [Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
20 | public static Validatable Throw(
21 | [DisallowNull, NotNull] this TValue value,
22 | Func exceptionThrower,
23 | [CallerArgumentExpression("value")] string? paramName = null)
24 | where TValue : notnull => new(value, paramName!, exceptionThrower);
25 |
26 | ///
27 | /// Creates a new instance with the specified value.
28 | /// The instance can be used to throw exceptions if the value matches a condition specified.
29 | ///
30 | /// The value to be validated.
31 | ///
32 | /// A function which receives the parameter name returns an excpetion.
33 | /// This function will be used to create the exception that will be thrown if a condition is matched.
34 | /// For example: paramName => throw new Exception($"Parameter name: {paramName}").
35 | ///
36 | /// Doesn't need to be specified. Will be populated by the compiler.
37 | ///
38 | /// This extension method is intended for non-nullable types.
39 | /// For nullable types, use the extension method.
40 | ///
41 | [Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
42 | public static Validatable Throw(
43 | [DisallowNull, NotNull] this TValue value,
44 | Func exceptionThrower,
45 | [CallerArgumentExpression("value")] string? paramName = null)
46 | where TValue : notnull => new(value, paramName!, exceptionThrower);
47 |
48 | ///
49 | /// Creates a new instance with the specified value.
50 | /// The instance can be used to throw exceptions if the value matches a condition specified.
51 | ///
52 | /// The value to be validated.
53 | /// Exception customizations. This can be used to supply a custom exception message which will be used instead of the default message.
54 | /// Doesn't need to be specified. Will be populated by the compiler.
55 | ///
56 | /// This extension method is intended for non-nullable types.
57 | /// For nullable types, use the extension method.
58 | ///
59 | [Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
60 | public static Validatable Throw(
61 | [DisallowNull, NotNull] this TValue value,
62 | ExceptionCustomizations? exceptionCustomizations = null,
63 | [CallerArgumentExpression("value")] string? paramName = null)
64 | where TValue : notnull => new(value, paramName!, exceptionCustomizations);
65 | }
66 |
--------------------------------------------------------------------------------
/src/ValidatableExtensions/StringProperties/ValidatableExtensions.StringProperties.Length.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for string properties.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the string returned from the given is longer than characters.
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfLongerThan(
16 | this in Validatable validatable,
17 | Func func,
18 | int length,
19 | [CallerArgumentExpression("func")] string? funcName = null)
20 | where TValue : notnull
21 | {
22 | Validator.ThrowIfLongerThan(
23 | func(validatable.Value),
24 | $"{validatable.ParamName}: {funcName}",
25 | validatable.ExceptionCustomizations,
26 | length);
27 |
28 | return ref validatable;
29 | }
30 |
31 | ///
32 | /// Throws an exception if the string returned from the given is shorter than characters.
33 | ///
34 | ///
35 | /// The default exception thrown is an .
36 | ///
37 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
38 | public static ref readonly Validatable IfShorterThan(
39 | this in Validatable validatable,
40 | Func func,
41 | int length,
42 | [CallerArgumentExpression("func")] string? funcName = null)
43 | where TValue : notnull
44 | {
45 | Validator.ThrowIfShorterThan(
46 | func(validatable.Value),
47 | $"{validatable.ParamName}: {funcName}",
48 | validatable.ExceptionCustomizations,
49 | length);
50 |
51 | return ref validatable;
52 | }
53 |
54 | ///
55 | /// Throws an exception if the length of string returned from the given is equal to .
56 | ///
57 | ///
58 | /// The default exception thrown is an .
59 | ///
60 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
61 | public static ref readonly Validatable IfLengthEquals(
62 | this in Validatable validatable,
63 | Func func,
64 | int length,
65 | [CallerArgumentExpression("func")] string? funcName = null)
66 | where TValue : notnull
67 | {
68 | Validator.ThrowIfLengthEquals(
69 | func(validatable.Value),
70 | $"{validatable.ParamName}: {funcName}",
71 | validatable.ExceptionCustomizations,
72 | length);
73 |
74 | return ref validatable;
75 | }
76 |
77 | ///
78 | /// Throws an exception if the length of string returned from the given is not equal to .
79 | ///
80 | ///
81 | /// The default exception thrown is an .
82 | ///
83 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
84 | public static ref readonly Validatable IfLengthNotEquals(
85 | this in Validatable validatable,
86 | Func func,
87 | int length,
88 | [CallerArgumentExpression("func")] string? funcName = null)
89 | where TValue : notnull
90 | {
91 | Validator.ThrowIfLengthNotEquals(
92 | func(validatable.Value),
93 | $"{validatable.ParamName}: {funcName}",
94 | validatable.ExceptionCustomizations,
95 | length);
96 |
97 | return ref validatable;
98 | }
99 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/StringProperties/ValidatableExtensions.StringProperties.Regex.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | using System.Text.RegularExpressions;
4 |
5 | ///
6 | /// Extension methods for string properties.
7 | ///
8 | public static partial class ValidatableExtensions
9 | {
10 | ///
11 | /// Throws an exception if the string returned from the given matches the given .
12 | /// Default is .
13 | ///
14 | ///
15 | /// The default exception thrown is an .
16 | ///
17 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
18 | public static ref readonly Validatable IfMatches(
19 | this in Validatable validatable,
20 | Func func,
21 | string regexPattern,
22 | RegexOptions regexOptions = RegexOptions.None,
23 | [CallerArgumentExpression("func")] string? funcName = null)
24 | where TValue : notnull
25 | {
26 | Validator.ThrowIfMatches(
27 | func(validatable.Value),
28 | $"{validatable.ParamName}: {funcName}",
29 | validatable.ExceptionCustomizations,
30 | regexPattern,
31 | regexOptions);
32 |
33 | return ref validatable;
34 | }
35 |
36 | ///
37 | /// Throws an exception if the string returned from the given matches the given .
38 | ///
39 | ///
40 | /// The default exception thrown is an .
41 | ///
42 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
43 | public static ref readonly Validatable IfMatches(
44 | this in Validatable validatable,
45 | Func func,
46 | Regex regex,
47 | [CallerArgumentExpression("func")] string? funcName = null)
48 | where TValue : notnull
49 | {
50 | Validator.ThrowIfMatches(
51 | func(validatable.Value),
52 | $"{validatable.ParamName}: {funcName}",
53 | validatable.ExceptionCustomizations,
54 | regex);
55 |
56 | return ref validatable;
57 | }
58 |
59 | ///
60 | /// Throws an exception if the string returned from the given does not match the given .
61 | /// Default is .
62 | ///
63 | ///
64 | /// The default exception thrown is an .
65 | ///
66 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
67 | public static ref readonly Validatable IfNotMatches(
68 | this in Validatable validatable,
69 | Func func,
70 | string regexPattern,
71 | RegexOptions regexOptions = RegexOptions.None,
72 | [CallerArgumentExpression("func")] string? funcName = null)
73 | where TValue : notnull
74 | {
75 | Validator.ThrowIfNotMatches(
76 | func(validatable.Value),
77 | $"{validatable.ParamName}: {funcName}",
78 | validatable.ExceptionCustomizations,
79 | regexPattern,
80 | regexOptions);
81 |
82 | return ref validatable;
83 | }
84 |
85 | ///
86 | /// Throws an exception if the string returned from the given does not match the given .
87 | ///
88 | ///
89 | /// The default exception thrown is an .
90 | ///
91 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
92 | public static ref readonly Validatable IfNotMatches(
93 | this in Validatable validatable,
94 | Func func,
95 | Regex regex,
96 | [CallerArgumentExpression("func")] string? funcName = null)
97 | where TValue : notnull
98 | {
99 | Validator.ThrowIfNotMatches(
100 | func(validatable.Value),
101 | $"{validatable.ParamName}: {funcName}",
102 | validatable.ExceptionCustomizations,
103 | regex);
104 |
105 | return ref validatable;
106 | }
107 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/StringProperties/ValidatableExtensions.StringProperties.Substring.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for string properties.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the string returned from the given ends with .
10 | /// Default is .
11 | ///
12 | ///
13 | /// The default exception thrown is an .
14 | ///
15 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
16 | public static ref readonly Validatable IfEndsWith(
17 | this in Validatable validatable,
18 | Func func,
19 | string str,
20 | StringComparison comparisonType = StringComparison.Ordinal,
21 | [CallerArgumentExpression("func")] string? funcName = null)
22 | where TValue : notnull
23 | {
24 | Validator.ThrowIfEndsWith(
25 | func(validatable.Value),
26 | $"{validatable.ParamName}: {funcName}",
27 | validatable.ExceptionCustomizations,
28 | str,
29 | comparisonType);
30 |
31 | return ref validatable;
32 | }
33 |
34 | ///
35 | /// Throws an exception if the string returned from the given does not end with .
36 | /// Default is .
37 | ///
38 | ///
39 | /// The default exception thrown is an .
40 | ///
41 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
42 | public static ref readonly Validatable IfNotEndsWith(
43 | this in Validatable validatable,
44 | Func func,
45 | string str,
46 | StringComparison comparisonType = StringComparison.Ordinal,
47 | [CallerArgumentExpression("func")] string? funcName = null)
48 | where TValue : notnull
49 | {
50 | Validator.ThrowIfNotEndsWith(
51 | func(validatable.Value),
52 | $"{validatable.ParamName}: {funcName}",
53 | validatable.ExceptionCustomizations,
54 | str,
55 | comparisonType);
56 |
57 | return ref validatable;
58 | }
59 |
60 | ///
61 | /// Throws an exception if the string returned from the given starts with .
62 | /// Default is .
63 | ///
64 | ///
65 | /// The default exception thrown is an .
66 | ///
67 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
68 | public static ref readonly Validatable IfStartsWith(
69 | this in Validatable validatable,
70 | Func func,
71 | string str,
72 | StringComparison comparisonType = StringComparison.Ordinal,
73 | [CallerArgumentExpression("func")] string? funcName = null)
74 | where TValue : notnull
75 | {
76 | Validator.ThrowIfStartsWith(
77 | func(validatable.Value),
78 | $"{validatable.ParamName}: {funcName}",
79 | validatable.ExceptionCustomizations,
80 | str,
81 | comparisonType);
82 |
83 | return ref validatable;
84 | }
85 |
86 | ///
87 | /// Throws an exception if the string returned from the given does not start with .
88 | /// Default is .
89 | ///
90 | ///
91 | /// The default exception thrown is an .
92 | ///
93 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
94 | public static ref readonly Validatable IfNotStartsWith(
95 | this in Validatable validatable,
96 | Func func,
97 | string str,
98 | StringComparison comparisonType = StringComparison.Ordinal,
99 | [CallerArgumentExpression("func")] string? funcName = null)
100 | where TValue : notnull
101 | {
102 | Validator.ThrowIfNotStartsWith(
103 | func(validatable.Value),
104 | $"{validatable.ParamName}: {funcName}",
105 | validatable.ExceptionCustomizations,
106 | str,
107 | comparisonType);
108 |
109 | return ref validatable;
110 | }
111 |
112 | ///
113 | /// Throws an exception if the string returned from the given contains the given .
114 | /// Default is .
115 | ///
116 | ///
117 | /// The default exception thrown is an .
118 | ///
119 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
120 | public static ref readonly Validatable IfContains(
121 | this in Validatable validatable,
122 | Func func,
123 | string otherString,
124 | StringComparison comparisonType = StringComparison.Ordinal,
125 | [CallerArgumentExpression("func")] string? funcName = null)
126 | where TValue : notnull
127 | {
128 | Validator.ThrowIfContains(
129 | func(validatable.Value),
130 | $"{validatable.ParamName}: {funcName}",
131 | validatable.ExceptionCustomizations,
132 | otherString,
133 | comparisonType);
134 |
135 | return ref validatable;
136 | }
137 |
138 | ///
139 | /// Throws an exception if the string returned from the given does not contain the given .
140 | /// Default is .
141 | ///
142 | ///
143 | /// The default exception thrown is an .
144 | ///
145 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
146 | public static ref readonly Validatable IfNotContains(
147 | this in Validatable validatable,
148 | Func func,
149 | string otherString,
150 | StringComparison comparisonType = StringComparison.Ordinal,
151 | [CallerArgumentExpression("func")] string? funcName = null)
152 | where TValue : notnull
153 | {
154 | Validator.ThrowIfNotContains(
155 | func(validatable.Value),
156 | $"{validatable.ParamName}: {funcName}",
157 | validatable.ExceptionCustomizations,
158 | otherString,
159 | comparisonType);
160 |
161 | return ref validatable;
162 | }
163 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/Strings/ValidatableExtensions.Strings.Equality.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for strings.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the string is white space only.
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfWhiteSpace(this in Validatable validatable)
16 | {
17 | Validator.ThrowIfWhiteSpace(validatable.Value, validatable.ParamName, validatable.ExceptionCustomizations);
18 |
19 | return ref validatable;
20 | }
21 |
22 | ///
23 | /// Throws an exception if the string is empty.
24 | ///
25 | ///
26 | /// The default exception thrown is an .
27 | ///
28 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
29 | public static ref readonly Validatable IfEmpty(this in Validatable validatable)
30 | {
31 | Validator.ThrowIfEmpty(validatable.Value, validatable.ParamName, validatable.ExceptionCustomizations);
32 |
33 | return ref validatable;
34 | }
35 |
36 | ///
37 | /// Throws an exception if the string equals the given .
38 | ///
39 | ///
40 | /// The used is .
41 | /// The default exception thrown is an .
42 | ///
43 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
44 | public static ref readonly Validatable IfEquals(this in Validatable validatable, string otherString)
45 | {
46 | Validator.ThrowIfEquals(
47 | validatable.Value,
48 | validatable.ParamName,
49 | validatable.ExceptionCustomizations,
50 | otherString,
51 | StringComparison.Ordinal);
52 |
53 | return ref validatable;
54 | }
55 |
56 | ///
57 | /// Throws an exception if the string equals the given using the given .
58 | ///
59 | ///
60 | /// The default exception thrown is an .
61 | ///
62 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
63 | public static ref readonly Validatable IfEquals(
64 | this in Validatable validatable,
65 | string otherString,
66 | StringComparison comparisonType)
67 | {
68 | Validator.ThrowIfEquals(
69 | validatable.Value,
70 | validatable.ParamName,
71 | validatable.ExceptionCustomizations,
72 | otherString,
73 | comparisonType);
74 |
75 | return ref validatable;
76 | }
77 |
78 | ///
79 | /// Throws an exception if the string does not equal the given .
80 | ///
81 | ///
82 | /// The used is .
83 | /// The default exception thrown is an .
84 | ///
85 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
86 | public static ref readonly Validatable IfNotEquals(
87 | this in Validatable validatable,
88 | string otherString)
89 | {
90 | Validator.ThrowIfNotEquals(
91 | validatable.Value,
92 | validatable.ParamName,
93 | validatable.ExceptionCustomizations,
94 | otherString,
95 | StringComparison.Ordinal);
96 |
97 | return ref validatable;
98 | }
99 |
100 | ///
101 | /// Throws an exception if the string does not equal the using the given .
102 | ///
103 | ///
104 | /// The default exception thrown is an .
105 | ///
106 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
107 | public static ref readonly Validatable IfNotEquals(this in Validatable validatable, string otherString, StringComparison comparisonType)
108 | {
109 | Validator.ThrowIfNotEquals(validatable.Value, validatable.ParamName, validatable.ExceptionCustomizations, otherString, comparisonType);
110 |
111 | return ref validatable;
112 | }
113 |
114 | ///
115 | /// Throws an exception if the string equals the given (case insensitive).
116 | ///
117 | ///
118 | /// The default exception thrown is an .
119 | ///
120 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
121 | public static ref readonly Validatable IfEqualsIgnoreCase(this in Validatable validatable, string otherString)
122 | {
123 | Validator.ThrowIfEquals(
124 | validatable.Value,
125 | validatable.ParamName,
126 | validatable.ExceptionCustomizations,
127 | otherString,
128 | StringComparison.OrdinalIgnoreCase);
129 |
130 | return ref validatable;
131 | }
132 |
133 | ///
134 | /// Throws an exception if the string does not equal the given (case insensitive).
135 | ///
136 | ///
137 | /// The used is .
138 | /// The default exception thrown is an .
139 | ///
140 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
141 | public static ref readonly Validatable IfNotEqualsIgnoreCase(this in Validatable validatable, string otherString)
142 | {
143 | Validator.ThrowIfNotEquals(
144 | validatable.Value,
145 | validatable.ParamName,
146 | validatable.ExceptionCustomizations,
147 | otherString,
148 | StringComparison.OrdinalIgnoreCase);
149 |
150 | return ref validatable;
151 | }
152 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/Strings/ValidatableExtensions.Strings.Length.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for strings.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the string is longer than characters.
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfLongerThan(this in Validatable validatable, int length)
16 | {
17 | Validator.ThrowIfLongerThan(
18 | validatable.Value,
19 | validatable.ParamName,
20 | validatable.ExceptionCustomizations,
21 | length);
22 |
23 | return ref validatable;
24 | }
25 |
26 | ///
27 | /// Throws an exception if the string is shortter than characters.
28 | ///
29 | ///
30 | /// The default exception thrown is an .
31 | ///
32 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
33 | public static ref readonly Validatable IfShorterThan(this in Validatable validatable, int length)
34 | {
35 | Validator.ThrowIfShorterThan(
36 | validatable.Value,
37 | validatable.ParamName,
38 | validatable.ExceptionCustomizations,
39 | length);
40 |
41 | return ref validatable;
42 | }
43 |
44 | ///
45 | /// Throws an exception if the string length is equal to .
46 | ///
47 | ///
48 | /// The default exception thrown is an .
49 | ///
50 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
51 | public static ref readonly Validatable IfLengthEquals(this in Validatable validatable, int length)
52 | {
53 | Validator.ThrowIfLengthEquals(
54 | validatable.Value,
55 | validatable.ParamName,
56 | validatable.ExceptionCustomizations,
57 | length);
58 |
59 | return ref validatable;
60 | }
61 |
62 | ///
63 | /// Throws an exception if the string length is not equal to .
64 | ///
65 | ///
66 | /// The default exception thrown is an .
67 | ///
68 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
69 | public static ref readonly Validatable IfLengthNotEquals(
70 | this in Validatable validatable,
71 | int length)
72 | {
73 | Validator.ThrowIfLengthNotEquals(
74 | validatable.Value,
75 | validatable.ParamName,
76 | validatable.ExceptionCustomizations,
77 | length);
78 |
79 | return ref validatable;
80 | }
81 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/Strings/ValidatableExtensions.Strings.Regex.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | using System.Text.RegularExpressions;
4 |
5 | ///
6 | /// Extension methods for strings.
7 | ///
8 | public static partial class ValidatableExtensions
9 | {
10 | ///
11 | /// Throws an exception if the string matches the given .
12 | /// Default is .
13 | ///
14 | ///
15 | /// The default exception thrown is an .
16 | ///
17 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
18 | public static ref readonly Validatable IfMatches(
19 | this in Validatable validatable,
20 | string regexPattern,
21 | RegexOptions regexOptions = RegexOptions.None)
22 | {
23 | Validator.ThrowIfMatches(
24 | validatable.Value,
25 | validatable.ParamName,
26 | validatable.ExceptionCustomizations,
27 | regexPattern,
28 | regexOptions);
29 |
30 | return ref validatable;
31 | }
32 |
33 | ///
34 | /// Throws an exception if the string matches the given .
35 | ///
36 | ///
37 | /// The default exception thrown is an .
38 | ///
39 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
40 | public static ref readonly Validatable IfMatches(this in Validatable validatable, Regex regex)
41 | {
42 | Validator.ThrowIfMatches(validatable.Value, validatable.ParamName, validatable.ExceptionCustomizations, regex);
43 |
44 | return ref validatable;
45 | }
46 |
47 | ///
48 | /// Throws an exception if the string does not match the given .
49 | /// Default is .
50 | ///
51 | ///
52 | /// The default exception thrown is an .
53 | ///
54 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
55 | public static ref readonly Validatable IfNotMatches(
56 | this in Validatable validatable,
57 | string regexPattern,
58 | RegexOptions regexOptions = RegexOptions.None)
59 | {
60 | Validator.ThrowIfNotMatches(
61 | validatable.Value,
62 | validatable.ParamName,
63 | validatable.ExceptionCustomizations,
64 | regexPattern,
65 | regexOptions);
66 |
67 | return ref validatable;
68 | }
69 |
70 | ///
71 | /// Throws an exception if the string does not match the given .
72 | ///
73 | ///
74 | /// The default exception thrown is an .
75 | ///
76 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
77 | public static ref readonly Validatable IfNotMatches(this in Validatable validatable, Regex regex)
78 | {
79 | Validator.ThrowIfNotMatches(
80 | validatable.Value,
81 | validatable.ParamName,
82 | validatable.ExceptionCustomizations,
83 | regex);
84 |
85 | return ref validatable;
86 | }
87 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/Strings/ValidatableExtensions.Strings.Substring.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for strings.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the string ends with .
10 | /// Default is .
11 | ///
12 | ///
13 | /// The default exception thrown is an .
14 | ///
15 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
16 | public static ref readonly Validatable IfEndsWith(
17 | this in Validatable validatable,
18 | string str,
19 | StringComparison comparisonType = StringComparison.Ordinal)
20 | {
21 | Validator.ThrowIfEndsWith(
22 | validatable.Value,
23 | validatable.ParamName,
24 | validatable.ExceptionCustomizations,
25 | str,
26 | comparisonType);
27 |
28 | return ref validatable;
29 | }
30 |
31 | ///
32 | /// Throws an exception if the string does not end with .
33 | /// Default is .
34 | ///
35 | ///
36 | /// The default exception thrown is an .
37 | ///
38 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
39 | public static ref readonly Validatable IfNotEndsWith(
40 | this in Validatable validatable,
41 | string str,
42 | StringComparison comparisonType = StringComparison.Ordinal)
43 | {
44 | Validator.ThrowIfNotEndsWith(
45 | validatable.Value,
46 | validatable.ParamName,
47 | validatable.ExceptionCustomizations,
48 | str,
49 | comparisonType);
50 |
51 | return ref validatable;
52 | }
53 |
54 | ///
55 | /// Throws an exception if the string starts with .
56 | /// Default is .
57 | ///
58 | ///
59 | /// The default exception thrown is an .
60 | ///
61 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
62 | public static ref readonly Validatable IfStartsWith(
63 | this in Validatable validatable,
64 | string str,
65 | StringComparison comparisonType = StringComparison.Ordinal)
66 | {
67 | Validator.ThrowIfStartsWith(
68 | validatable.Value,
69 | validatable.ParamName,
70 | validatable.ExceptionCustomizations,
71 | str,
72 | comparisonType);
73 |
74 | return ref validatable;
75 | }
76 |
77 | ///
78 | /// Throws an exception if the string does not start with .
79 | /// Default is .
80 | ///
81 | ///
82 | /// The default exception thrown is an .
83 | ///
84 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
85 | public static ref readonly Validatable IfNotStartsWith(
86 | this in Validatable validatable,
87 | string str,
88 | StringComparison comparisonType = StringComparison.Ordinal)
89 | {
90 | Validator.ThrowIfNotStartsWith(
91 | validatable.Value,
92 | validatable.ParamName,
93 | validatable.ExceptionCustomizations,
94 | str,
95 | comparisonType);
96 |
97 | return ref validatable;
98 | }
99 |
100 | ///
101 | /// Throws an exception if the string contains the given .
102 | /// Default is .
103 | ///
104 | ///
105 | /// The default exception thrown is an .
106 | ///
107 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
108 | public static ref readonly Validatable IfContains(
109 | this in Validatable validatable,
110 | string otherString,
111 | StringComparison comparisonType = StringComparison.Ordinal)
112 | {
113 | Validator.ThrowIfContains(
114 | validatable.Value,
115 | validatable.ParamName,
116 | validatable.ExceptionCustomizations,
117 | otherString,
118 | comparisonType);
119 |
120 | return ref validatable;
121 | }
122 |
123 | ///
124 | /// Throws an exception if the string does not contain the given .
125 | /// Default is .
126 | ///
127 | ///
128 | /// The default exception thrown is an .
129 | ///
130 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
131 | public static ref readonly Validatable IfNotContains(
132 | this in Validatable validatable,
133 | string otherString,
134 | StringComparison comparisonType = StringComparison.Ordinal)
135 | {
136 | Validator.ThrowIfNotContains(
137 | validatable.Value,
138 | validatable.ParamName,
139 | validatable.ExceptionCustomizations,
140 | otherString,
141 | comparisonType);
142 |
143 | return ref validatable;
144 | }
145 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.BooleanProperties.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for boolean properties.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the boolean value returned from the given is true.
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfTrue(
16 | this in Validatable validatable,
17 | Func func,
18 | [CallerArgumentExpression("func")] string? funcName = null)
19 | where TValue : notnull
20 | {
21 | if (func(validatable.Value))
22 | {
23 | ExceptionThrower.Throw(
24 | validatable.ParamName,
25 | validatable.ExceptionCustomizations,
26 | $"Value should not meet condition (condition: '{funcName}').");
27 | }
28 |
29 | return ref validatable;
30 | }
31 |
32 | ///
33 | /// Throws an exception if the boolean value returned from the given is false.
34 | ///
35 | ///
36 | /// The default exception thrown is an .
37 | ///
38 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
39 | public static ref readonly Validatable IfFalse(
40 | this in Validatable validatable,
41 | Func func,
42 | [CallerArgumentExpression("func")] string? funcName = null)
43 | where TValue : notnull
44 | {
45 | if (!func(validatable.Value))
46 | {
47 | ExceptionThrower.Throw(
48 | validatable.ParamName,
49 | validatable.ExceptionCustomizations,
50 | $"Value should meet condition (condition: '{funcName}').");
51 | }
52 |
53 | return ref validatable;
54 | }
55 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.Booleans.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for booleans.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the boolean value is true.
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfTrue(this in Validatable validatable)
16 | {
17 | if (validatable.Value)
18 | {
19 | ExceptionThrower.Throw(
20 | validatable.ParamName,
21 | validatable.ExceptionCustomizations,
22 | "Value should not be true.");
23 | }
24 |
25 | return ref validatable;
26 | }
27 |
28 | ///
29 | /// Throws an exception if the boolean value is false.
30 | ///
31 | ///
32 | /// The default exception thrown is an .
33 | ///
34 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
35 | public static ref readonly Validatable IfFalse(this in Validatable validatable)
36 | {
37 | if (!validatable.Value)
38 | {
39 | ExceptionThrower.Throw(
40 | validatable.ParamName,
41 | validatable.ExceptionCustomizations,
42 | "Value should be true.");
43 | }
44 |
45 | return ref validatable;
46 | }
47 |
48 | ///
49 | /// Throws an exception if the is true.
50 | ///
51 | ///
52 | /// The default exception thrown is an .
53 | ///
54 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
55 | public static ref readonly Validatable IfTrue(
56 | this in Validatable validatable,
57 | bool condition,
58 | [CallerArgumentExpression("condition")] string? conditionParamName = null)
59 | where TValue : notnull
60 | {
61 | if (condition)
62 | {
63 | ExceptionThrower.Throw(
64 | validatable.ParamName,
65 | validatable.ExceptionCustomizations,
66 | $"Value should not meet condition (condition: '{conditionParamName}').");
67 | }
68 |
69 | return ref validatable;
70 | }
71 |
72 | ///
73 | /// Throws an exception if the is false.
74 | ///
75 | ///
76 | /// The default exception thrown is an .
77 | ///
78 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
79 | public static ref readonly Validatable IfFalse(
80 | this in Validatable validatable,
81 | bool condition,
82 | [CallerArgumentExpression("condition")] string? conditionParamName = null)
83 | where TValue : notnull
84 | {
85 | if (!condition)
86 | {
87 | ExceptionThrower.Throw(
88 | validatable.ParamName,
89 | validatable.ExceptionCustomizations,
90 | $"Value should meet condition (condition: '{conditionParamName}').");
91 | }
92 |
93 | return ref validatable;
94 | }
95 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.Collections.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for collections.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the collection is empty.
10 | /// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
11 | ///
12 | ///
13 | /// The default exception thrown is an .
14 | ///
15 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
16 | public static ref readonly Validatable IfEmpty(this in Validatable validatable)
17 | where TValue : notnull, IEnumerable
18 | {
19 | Validator.ThrowIfCount(
20 | validatable.Value,
21 | 0,
22 | validatable.ParamName,
23 | validatable.ExceptionCustomizations,
24 | "Collection should not be empty.");
25 |
26 | return ref validatable;
27 | }
28 |
29 | ///
30 | /// Throws an exception if the collection is not empty.
31 | /// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
32 | ///
33 | ///
34 | /// The default exception thrown is an .
35 | ///
36 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
37 | public static ref readonly Validatable IfNotEmpty(this in Validatable validatable)
38 | where TValue : notnull, IEnumerable
39 | {
40 | Validator.ThrowIfCountNot(
41 | validatable.Value,
42 | 0,
43 | validatable.ParamName,
44 | validatable.ExceptionCustomizations,
45 | "Collection should be empty.");
46 |
47 | return ref validatable;
48 | }
49 |
50 | ///
51 | /// Throws an exception if the collection count does not match the specified .
52 | /// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
53 | ///
54 | ///
55 | /// The default exception thrown is an .
56 | ///
57 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
58 | public static ref readonly Validatable IfCountNotEquals(
59 | this in Validatable validatable,
60 | int count)
61 | where TValue : notnull, IEnumerable
62 | {
63 | Validator.ThrowIfCountNot(validatable.Value, count, validatable.ParamName, validatable.ExceptionCustomizations);
64 |
65 | return ref validatable;
66 | }
67 |
68 | ///
69 | /// Throws an exception if the collection count matches the specified .
70 | /// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
71 | ///
72 | ///
73 | /// The default exception thrown is an .
74 | ///
75 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
76 | public static ref readonly Validatable IfCountEquals(
77 | this in Validatable validatable,
78 | int count)
79 | where TValue : notnull, IEnumerable
80 | {
81 | Validator.ThrowIfCount(validatable.Value, count, validatable.ParamName, validatable.ExceptionCustomizations);
82 |
83 | return ref validatable;
84 | }
85 |
86 | ///
87 | /// Throws an exception if the collection count is greater than the specified .
88 | /// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
89 | ///
90 | ///
91 | /// The default exception thrown is an .
92 | ///
93 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
94 | public static ref readonly Validatable IfCountGreaterThan(
95 | this in Validatable validatable,
96 | int count)
97 | where TValue : notnull, IEnumerable
98 | {
99 | Validator.ThrowIfCountGreaterThan(
100 | validatable.Value,
101 | count,
102 | validatable.ParamName,
103 | validatable.ExceptionCustomizations);
104 |
105 | return ref validatable;
106 | }
107 |
108 | ///
109 | /// Throws an exception if the collection count is less than the specified .
110 | /// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
111 | ///
112 | ///
113 | /// The default exception thrown is an .
114 | ///
115 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
116 | public static ref readonly Validatable IfCountLessThan(
117 | this in Validatable validatable,
118 | int count)
119 | where TValue : notnull, IEnumerable
120 | {
121 | Validator.ThrowIfCountLessThan(
122 | validatable.Value,
123 | count,
124 | validatable.ParamName,
125 | validatable.ExceptionCustomizations);
126 |
127 | return ref validatable;
128 | }
129 |
130 | ///
131 | /// Throws an exception if the collection contains null elements.
132 | /// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
133 | ///
134 | ///
135 | /// The default exception thrown is an .
136 | ///
137 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
138 | public static ref readonly Validatable IfHasNullElements(this in Validatable validatable)
139 | where TValue : notnull, IEnumerable
140 | {
141 | Validator.ThrowIfHasNullElements(validatable.Value, validatable.ParamName, validatable.ExceptionCustomizations);
142 |
143 | return ref validatable;
144 | }
145 |
146 | ///
147 | /// Throws an exception if the collection contains the given .
148 | /// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
149 | ///
150 | ///
151 | /// The default exception thrown is an .
152 | ///
153 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
154 | public static ref readonly Validatable IfContains(
155 | this in Validatable validatable,
156 | TElement element)
157 | where TValue : notnull, IEnumerable
158 | where TElement : notnull
159 | {
160 | Validator.ThrowIfContainsElement(
161 | validatable.Value,
162 | element,
163 | validatable.ParamName,
164 | validatable.ExceptionCustomizations);
165 |
166 | return ref validatable;
167 | }
168 |
169 | ///
170 | /// Throws an exception if the collection does not contain the given .
171 | /// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
172 | ///
173 | ///
174 | /// The default exception thrown is an .
175 | ///
176 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
177 | public static ref readonly Validatable IfNotContains(
178 | this in Validatable validatable,
179 | TElement element)
180 | where TValue : notnull, IEnumerable
181 | where TElement : notnull
182 | {
183 | Validator.ThrowIfNotContainsElement(
184 | validatable.Value,
185 | element,
186 | validatable.ParamName,
187 | validatable.ExceptionCustomizations);
188 |
189 | return ref validatable;
190 | }
191 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.Comparables.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for comparables (int, double, decimal, long, float, short, DateTime, DateOnly, TimeOnly etc.).
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the value is greater than .
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfGreaterThan(
16 | this in Validatable validatable,
17 | TValue n)
18 | where TValue : notnull, IComparable
19 | {
20 | Validator.ThrowIfGreaterThan(validatable.Value, n, validatable.ParamName, validatable.ExceptionCustomizations);
21 |
22 | return ref validatable;
23 | }
24 |
25 | ///
26 | /// Throws an exception if the value is greater than or equal to .
27 | ///
28 | ///
29 | /// The default exception thrown is an .
30 | ///
31 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
32 | public static ref readonly Validatable IfGreaterThanOrEqualTo(
33 | this in Validatable validatable,
34 | TValue n)
35 | where TValue : notnull, IComparable
36 | {
37 | Validator.ThrowIfGreaterThanOrEqualTo(validatable.Value, n, validatable.ParamName, validatable.ExceptionCustomizations);
38 |
39 | return ref validatable;
40 | }
41 |
42 | ///
43 | /// Throws an exception if the value is less than .
44 | ///
45 | ///
46 | /// The default exception thrown is an .
47 | ///
48 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
49 | public static ref readonly Validatable IfLessThan(this in Validatable validatable, TValue n)
50 | where TValue : notnull, IComparable
51 | {
52 | Validator.ThrowIfLessThan(validatable.Value, n, validatable.ParamName, validatable.ExceptionCustomizations);
53 |
54 | return ref validatable;
55 | }
56 |
57 | ///
58 | /// Throws an exception if the value is less than or equal to .
59 | ///
60 | ///
61 | /// The default exception thrown is an .
62 | ///
63 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
64 | public static ref readonly Validatable IfLessThanOrEqualTo(this in Validatable validatable, TValue n)
65 | where TValue : notnull, IComparable
66 | {
67 | Validator.ThrowIfLessThanOrEqualTo(validatable.Value, n, validatable.ParamName, validatable.ExceptionCustomizations);
68 |
69 | return ref validatable;
70 | }
71 |
72 | ///
73 | /// Throws an exception if the value is greater than 0.
74 | ///
75 | ///
76 | /// The default exception thrown is an .
77 | ///
78 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
79 | public static ref readonly Validatable IfPositive(this in Validatable validatable)
80 | where TValue : notnull, IComparable
81 | {
82 | Validator.ThrowIfGreaterThan(
83 | validatable.Value,
84 | default!,
85 | validatable.ParamName,
86 | validatable.ExceptionCustomizations);
87 |
88 | return ref validatable;
89 | }
90 |
91 | ///
92 | /// Throws an exception if the value is greater than or equal to 0.
93 | ///
94 | ///
95 | /// The default exception thrown is an .
96 | ///
97 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
98 | public static ref readonly Validatable IfPositiveOrZero(this in Validatable validatable)
99 | where TValue : notnull, IComparable
100 | {
101 | Validator.ThrowIfGreaterThanOrEqualTo(
102 | validatable.Value,
103 | default!,
104 | validatable.ParamName,
105 | validatable.ExceptionCustomizations);
106 |
107 | return ref validatable;
108 | }
109 |
110 | ///
111 | /// Throws an exception if the value is less than 0.
112 | ///
113 | ///
114 | /// The default exception thrown is an .
115 | ///
116 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
117 | public static ref readonly Validatable IfNegative(this in Validatable validatable)
118 | where TValue : notnull, IComparable
119 | {
120 | Validator.ThrowIfLessThan(
121 | validatable.Value,
122 | default!,
123 | validatable.ParamName,
124 | validatable.ExceptionCustomizations);
125 |
126 | return ref validatable;
127 | }
128 |
129 | ///
130 | /// Throws an exception if the value is less than or equal to 0.
131 | ///
132 | ///
133 | /// The default exception thrown is an .
134 | ///
135 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
136 | public static ref readonly Validatable IfNegativeOrZero(this in Validatable validatable)
137 | where TValue : notnull, IComparable
138 | {
139 | Validator.ThrowIfLessThanOrEqualTo(
140 | validatable.Value,
141 | default!,
142 | validatable.ParamName,
143 | validatable.ExceptionCustomizations);
144 |
145 | return ref validatable;
146 | }
147 |
148 | ///
149 | /// Throws an exception if the value is not between and .
150 | ///
151 | /// The validatable being validated.
152 | /// The minimum value, inclusive (equals or greater than).
153 | /// The maximum value, inclusive (equals or less than).
154 | ///
155 | /// The default exception thrown is an .
156 | ///
157 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
158 | public static ref readonly Validatable IfOutOfRange(
159 | this in Validatable validatable,
160 | TValue min,
161 | TValue max)
162 | where TValue : notnull, IComparable
163 | {
164 | Validator.ThrowIfNotInRange(
165 | validatable.Value,
166 | min,
167 | max,
168 | validatable.ParamName,
169 | validatable.ExceptionCustomizations);
170 |
171 | return ref validatable;
172 | }
173 |
174 | ///
175 | /// Throws an exception if the value is between and .
176 | ///
177 | /// The validatable being validated.
178 | /// The minimum value, inclusive (equals or greater than).
179 | /// The maximum value, inclusive (equals or less than).
180 | ///
181 | /// The default exception thrown is an .
182 | ///
183 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
184 | public static ref readonly Validatable IfInRange(
185 | this in Validatable validatable,
186 | TValue min,
187 | TValue max)
188 | where TValue : notnull, IComparable
189 | {
190 | Validator.ThrowIfInRange(
191 | validatable.Value,
192 | min,
193 | max,
194 | validatable.ParamName,
195 | validatable.ExceptionCustomizations);
196 |
197 | return ref validatable;
198 | }
199 | }
200 |
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.ConditionalCompilation.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | using System.Diagnostics;
4 |
5 | ///
6 | /// Extension methods for controlling whether the "throw" rule will be ignored by the compiler.
7 | ///
8 | public static partial class ValidatableExtensions
9 | {
10 | ///
11 | /// Removes the entire "throw" rule when the build is not debug.
12 | ///
13 | [Conditional("DEBUG")]
14 | public static void OnlyInDebug(this in Validatable validatable)
15 | where TValue : notnull
16 | {
17 | }
18 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.DateTimeProperties.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for properties.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the of the returned
10 | /// from the given is .
11 | ///
12 | ///
13 | /// The default exception thrown is an .
14 | ///
15 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
16 | public static ref readonly Validatable IfUtc(
17 | this in Validatable validatable,
18 | Func func,
19 | [CallerArgumentExpression("func")] string? funcName = null)
20 | where TValue : notnull
21 | {
22 | Validator.ThrowIfKind(
23 | func(validatable.Value),
24 | DateTimeKind.Utc,
25 | $"{validatable.ParamName}: {funcName}",
26 | validatable.ExceptionCustomizations);
27 |
28 | return ref validatable;
29 | }
30 |
31 | ///
32 | /// Throws an exception if the of the returned
33 | /// from the given is not .
34 | ///
35 | ///
36 | /// The default exception thrown is an .
37 | ///
38 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
39 | public static ref readonly Validatable IfNotUtc(
40 | this in Validatable validatable,
41 | Func func,
42 | [CallerArgumentExpression("func")] string? funcName = null)
43 | where TValue : notnull
44 | {
45 | Validator.ThrowIfNotKind(
46 | func(validatable.Value),
47 | DateTimeKind.Utc,
48 | $"{validatable.ParamName}: {funcName}",
49 | validatable.ExceptionCustomizations);
50 |
51 | return ref validatable;
52 | }
53 |
54 | ///
55 | /// Throws an exception if the of the returned
56 | /// from the given matches the given .
57 | ///
58 | ///
59 | /// The default exception thrown is an .
60 | ///
61 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
62 | public static ref readonly Validatable IfDateTimeKind(
63 | this in Validatable validatable,
64 | Func func,
65 | DateTimeKind kind,
66 | [CallerArgumentExpression("func")] string? funcName = null)
67 | where TValue : notnull
68 | {
69 | Validator.ThrowIfKind(
70 | func(validatable.Value),
71 | kind,
72 | $"{validatable.ParamName}: {funcName}",
73 | validatable.ExceptionCustomizations);
74 |
75 | return ref validatable;
76 | }
77 |
78 | ///
79 | /// Throws an exception if the of the returned
80 | /// from the given does not match the given .
81 | ///
82 | ///
83 | /// The default exception thrown is an .
84 | ///
85 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
86 | public static ref readonly Validatable IfDateTimeKindNot(
87 | this in Validatable validatable,
88 | Func func,
89 | DateTimeKind kind,
90 | [CallerArgumentExpression("func")] string? funcName = null)
91 | where TValue : notnull
92 | {
93 | Validator.ThrowIfNotKind(
94 | func(validatable.Value),
95 | kind,
96 | $"{validatable.ParamName}: {funcName}",
97 | validatable.ExceptionCustomizations);
98 |
99 | return ref validatable;
100 | }
101 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.DateTimes.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for s.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the is .
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfUtc(this in Validatable validatable)
16 | {
17 | Validator.ThrowIfKind(
18 | validatable.Value,
19 | DateTimeKind.Utc,
20 | validatable.ParamName,
21 | validatable.ExceptionCustomizations);
22 |
23 | return ref validatable;
24 | }
25 |
26 | ///
27 | /// Throws an exception if the is not .
28 | ///
29 | ///
30 | /// The default exception thrown is an .
31 | ///
32 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
33 | public static ref readonly Validatable IfNotUtc(this in Validatable validatable)
34 | {
35 | Validator.ThrowIfNotKind(
36 | validatable.Value,
37 | DateTimeKind.Utc,
38 | validatable.ParamName,
39 | validatable.ExceptionCustomizations);
40 |
41 | return ref validatable;
42 | }
43 |
44 | ///
45 | /// Throws an exception if the matches the given .
46 | ///
47 | ///
48 | /// The default exception thrown is an .
49 | ///
50 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
51 | public static ref readonly Validatable IfDateTimeKind(
52 | this in Validatable validatable,
53 | DateTimeKind kind)
54 | {
55 | Validator.ThrowIfKind(validatable.Value, kind, validatable.ParamName, validatable.ExceptionCustomizations);
56 |
57 | return ref validatable;
58 | }
59 |
60 | ///
61 | /// Throws an exception if the does not match the given .
62 | ///
63 | ///
64 | /// The default exception thrown is an .
65 | ///
66 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
67 | public static ref readonly Validatable IfDateTimeKindNot(
68 | this in Validatable validatable,
69 | DateTimeKind kind)
70 | {
71 | Validator.ThrowIfNotKind(validatable.Value, kind, validatable.ParamName, validatable.ExceptionCustomizations);
72 |
73 | return ref validatable;
74 | }
75 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.EnumProperties.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for enum properties.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the enum value returned from the given
10 | /// is not defined in the enum.
11 | ///
12 | ///
13 | /// The default exception thrown is an .
14 | ///
15 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
16 | public static ref readonly Validatable IfOutOfRange(
17 | this in Validatable validatable,
18 | Func func,
19 | [CallerArgumentExpression("func")] string? funcName = null)
20 | where TValue : notnull
21 | where TEnumValue : struct, Enum
22 | {
23 | Validator.ThrowIfOutOfRange(
24 | func(validatable.Value),
25 | $"{validatable.ParamName}: {funcName}",
26 | validatable.ExceptionCustomizations);
27 |
28 | return ref validatable;
29 | }
30 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.Enums.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for enums.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the enum value is not defined in the enum.
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfOutOfRange(this in Validatable validatable)
16 | where TValue : struct, Enum
17 | {
18 | Validator.ThrowIfOutOfRange(validatable.Value, validatable.ParamName, validatable.ExceptionCustomizations);
19 |
20 | return ref validatable;
21 | }
22 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.Equalities.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for equalities.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the value is equal to the default value of type .
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfDefault(this in Validatable validatable)
16 | where TValue : struct
17 | {
18 | Validator.ThrowIfEquals(
19 | validatable.Value,
20 | default,
21 | validatable.ParamName,
22 | validatable.ExceptionCustomizations,
23 | "Value should not be default.");
24 |
25 | return ref validatable;
26 | }
27 |
28 | ///
29 | /// Throws an exception if the value is not equal to the default value of type .
30 | ///
31 | ///
32 | /// The default exception thrown is an .
33 | ///
34 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
35 | public static ref readonly Validatable IfNotDefault(this in Validatable validatable)
36 | where TValue : struct
37 | {
38 | Validator.ThrowIfNotEquals(
39 | validatable.Value,
40 | default,
41 | validatable.ParamName,
42 | validatable.ExceptionCustomizations,
43 | "Value should be default.");
44 |
45 | return ref validatable;
46 | }
47 |
48 | ///
49 | /// Throws an exception if the value equals .
50 | ///
51 | ///
52 | /// The default exception thrown is an .
53 | ///
54 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
55 | public static ref readonly Validatable IfEquals(this in Validatable validatable, TValue other)
56 | where TValue : notnull
57 | {
58 | Validator.ThrowIfEquals(validatable.Value, other, validatable.ParamName, validatable.ExceptionCustomizations);
59 |
60 | return ref validatable;
61 | }
62 |
63 | ///
64 | /// Throws an exception if the value does not equal .
65 | ///
66 | ///
67 | /// The default exception thrown is an .
68 | ///
69 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
70 | public static ref readonly Validatable IfNotEquals(
71 | this in Validatable validatable,
72 | TValue other)
73 | where TValue : notnull
74 | {
75 | Validator.ThrowIfNotEquals(validatable.Value, other, validatable.ParamName, validatable.ExceptionCustomizations);
76 |
77 | return ref validatable;
78 | }
79 | }
--------------------------------------------------------------------------------
/src/ValidatableExtensions/ValidatableExtensions.EqualityProperties.cs:
--------------------------------------------------------------------------------
1 | namespace Throw;
2 |
3 | ///
4 | /// Extension methods for property equalities.
5 | ///
6 | public static partial class ValidatableExtensions
7 | {
8 | ///
9 | /// Throws an exception if the value returned from the given is equal to null.
10 | ///
11 | ///
12 | /// The default exception thrown is an .
13 | ///
14 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
15 | public static ref readonly Validatable IfNull(
16 | this in Validatable validatable,
17 | Func func,
18 | [CallerArgumentExpression("func")] string? funcName = null)
19 | where TValue : notnull
20 | {
21 | if (func(validatable.Value) is null)
22 | {
23 | ExceptionThrower.ThrowNull($"{validatable.ParamName}: {funcName}", validatable.ExceptionCustomizations);
24 | }
25 |
26 | return ref validatable;
27 | }
28 |
29 | ///
30 | /// Throws an exception if the value returned from the given