Skip to content

Commit

Permalink
Merge pull request #15 from luboshl/feature/validation-settings
Browse files Browse the repository at this point in the history
Opt-out validation of non-nullable reference types
  • Loading branch information
luboshl authored Jul 24, 2024
2 parents 59eb2c7 + 3fae00d commit a8a79c3
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 95 deletions.
97 changes: 52 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,58 @@ class Widget
}
```

### Skip validation

You can set property to be skipped when validation is performed by setting `SkipValidationAttribute`
on it. If you want to perform validation on the property, but not to validate it recursively
(validate properties of the property), you can set `SkipRecursionAttribute` on it.

When you use `SkipValidationAttribute` on a property, recursion is also skipped for that property.

```csharp
class Model
{
[SkipValidation]
public string Name => throw new InvalidOperationException();

public ValidChild ValidChild { get; set; }

public ValidChildWithInvalidSkippedProperty ValidChildWithInvalidSkippedProperty { get; set; }

[SkipRecursion]
public InvalidChild InvalidChild { get; set; }
}

class ValidChild
{
[Range(10, 100)]
public int TenOrMore { get; set; } = 10;
}

class ValidChildWithInvalidSkippedProperty
{
// Property is invalid but is skipped
[SkipValidation]
public string Name { get; set; } = null!;
}

class InvalidChild
{
// Property is invalid
[Range(10, 100)]
public int TenOrMore { get; set; } = 3;
}
```

### Opt-out validation of non-nullable reference types

You can disable validation of properties that are non-nullable reference types by configuring `ValidationSettings`:

```csharp
var validationSettings = ValidationSettings.Default with { ValidateNonNullableReferenceTypes = false };
var isValid = MiniValidatorPlus.TryValidate(objectToValidate, validationSettings, out var errors);
```

### Use services from validators

```csharp
Expand Down Expand Up @@ -217,48 +269,3 @@ class WidgetWithCustomValidation : Widget, IValidatableObject
}
}
```

## Skip validation

You can set property to be skipped when validation is performed by setting `SkipValidationAttribute`
on it. If you want to perform validation on the property, but not to validate it recursively
(validate properties of the property), you can set `SkipRecursionAttribute` on it.

When you use `SkipValidationAttribute` on a property, recursion is also skipped for that property.

### Examples

```csharp
class Model
{
[SkipValidation]
public string Name => throw new InvalidOperationException();

public ValidChild ValidChild { get; set; }

public ValidChildWithInvalidSkippedProperty ValidChildWithInvalidSkippedProperty { get; set; }

[SkipRecursion]
public InvalidChild InvalidChild { get; set; }
}

class ValidChild
{
[Range(10, 100)]
public int TenOrMore { get; set; } = 10;
}

class ValidChildWithInvalidSkippedProperty
{
// Property is invalid but is skipped
[SkipValidation]
public string Name { get; set; } = null!;
}

class InvalidChild
{
// Property is invalid
[Range(10, 100)]
public int TenOrMore { get; set; } = 3;
}
```
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionPrefix>1.1.0</VersionPrefix>
<VersionPrefix>1.2.0</VersionPrefix>
<!-- VersionSuffix used for local builds -->
<VersionSuffix>dev</VersionSuffix>
<!-- VersionSuffix to be used for CI builds -->
Expand Down
Loading

0 comments on commit a8a79c3

Please sign in to comment.