-
Notifications
You must be signed in to change notification settings - Fork 5k
Add new System.ComponentModel.DataAnnotations features #82311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
75ac3de
96b6c9e
114acf9
2c33daa
d0ae0cb
e761f6e
abecdf1
5aa0f31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.ComponentModel.DataAnnotations | ||
{ | ||
/// <summary> | ||
/// Specifies a list of values that should be allowed in a property. | ||
/// </summary> | ||
[CLSCompliant(false)] | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, | ||
AllowMultiple = false)] | ||
public class AllowedValuesAttribute : ValidationAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AllowedValuesAttribute"/> class. | ||
/// </summary> | ||
/// <param name="values"> | ||
/// A list of values that the validated value should be equal to. | ||
/// </param> | ||
public AllowedValuesAttribute(params object?[] values) | ||
{ | ||
ArgumentNullException.ThrowIfNull(values); | ||
Values = values; | ||
DefaultErrorMessage = SR.AllowedValuesAttribute_Invalid; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the list of values allowed by this attribute. | ||
/// </summary> | ||
public object?[] Values { get; } | ||
|
||
/// <summary> | ||
/// Determines whether a specified object is valid. (Overrides <see cref="ValidationAttribute.IsValid(object)" />) | ||
/// </summary> | ||
/// <param name="value">The object to validate.</param> | ||
/// <returns> | ||
/// <see langword="true" /> if any of the <see cref="Values"/> are equal to <paramref name="value"/>, | ||
/// otherwise <see langword="false" /> | ||
/// </returns> | ||
/// <remarks> | ||
/// This method can return <see langword="true"/> if the <paramref name="value" /> is <see langword="null"/>, | ||
/// provided that <see langword="null"/> is also specified in one of the <see cref="Values"/>. | ||
/// </remarks> | ||
public override bool IsValid(object? value) | ||
{ | ||
foreach (object? allowed in Values) | ||
{ | ||
if (allowed is null ? value is null : allowed.Equals(value)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
|
||
namespace System.ComponentModel.DataAnnotations | ||
{ | ||
/// <summary> | ||
/// Specifies that a data field value is a well-formed Base64 string. | ||
/// </summary> | ||
/// <remarks> | ||
/// Recognition of valid Base64 is delegated to the <see cref="Convert"/> class, | ||
/// using the <see cref="Convert.TryFromBase64String(string, Span{byte}, out int)"/> method. | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] | ||
public class Base64StringAttribute : ValidationAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Base64StringAttribute"/> class. | ||
/// </summary> | ||
public Base64StringAttribute() | ||
{ | ||
// Set DefaultErrorMessage not ErrorMessage, allowing user to set | ||
// ErrorMessageResourceType and ErrorMessageResourceName to use localized messages. | ||
DefaultErrorMessage = SR.Base64StringAttribute_Invalid; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether a specified object is valid. (Overrides <see cref="ValidationAttribute.IsValid(object)" />) | ||
/// </summary> | ||
/// <param name="value">The object to validate.</param> | ||
/// <returns> | ||
/// <see langword="true" /> if <paramref name="value"/> is <see langword="null"/> or is a valid Base64 string, | ||
/// otherwise <see langword="false" /> | ||
/// </returns> | ||
public override bool IsValid(object? value) | ||
{ | ||
if (value is null) | ||
{ | ||
return true; | ||
eiriktsarpalis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (value is not string valueAsString) | ||
{ | ||
return false; | ||
} | ||
|
||
byte[]? rentedBuffer = null; | ||
Span<byte> destinationBuffer = valueAsString.Length < 256 | ||
? stackalloc byte[256] | ||
: rentedBuffer = ArrayPool<byte>.Shared.Rent(valueAsString.Length); | ||
|
||
bool result = Convert.TryFromBase64String(valueAsString, destinationBuffer, out int bytesWritten); | ||
|
||
if (rentedBuffer != null) | ||
{ | ||
destinationBuffer.Slice(0, bytesWritten).Clear(); | ||
ArrayPool<byte>.Shared.Return(rentedBuffer); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.ComponentModel.DataAnnotations | ||
{ | ||
/// <summary> | ||
/// Specifies a list of values that should not be allowed in a property. | ||
/// </summary> | ||
[CLSCompliant(false)] | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, | ||
AllowMultiple = false)] | ||
public class DeniedValuesAttribute : ValidationAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DeniedValuesAttribute"/> class. | ||
/// </summary> | ||
/// <param name="values"> | ||
/// A list of values that the validated value should not be equal to. | ||
/// </param> | ||
public DeniedValuesAttribute(params object?[] values) | ||
{ | ||
ArgumentNullException.ThrowIfNull(values); | ||
Values = values; | ||
DefaultErrorMessage = SR.DeniedValuesAttribute_Invalid; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the list of values denied by this attribute. | ||
/// </summary> | ||
public object?[] Values { get; } | ||
|
||
/// <summary> | ||
/// Determines whether a specified object is valid. (Overrides <see cref="ValidationAttribute.IsValid(object)" />) | ||
/// </summary> | ||
/// <param name="value">The object to validate.</param> | ||
/// <returns> | ||
/// <see langword="true" /> if none of the <see cref="Values"/> are equal to <paramref name="value"/>, | ||
/// otherwise <see langword="false" />. | ||
/// </returns> | ||
/// <remarks> | ||
/// This method can return <see langword="true"/> if the <paramref name="value" /> is <see langword="null"/>, | ||
/// provided that <see langword="null"/> is not specified in any of the <see cref="Values"/>. | ||
/// </remarks> | ||
public override bool IsValid(object? value) | ||
{ | ||
foreach (object? allowed in Values) | ||
{ | ||
if (allowed is null ? value is null : allowed.Equals(value)) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Shouldn't the variable name be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah good point. Would you be interested in contributing a fix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How? I'm newbie here. Don't know the best approach. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that the change is fairly simple, you might be able to send a PR directly from the browser. Open up And hit the edit button: Make the change and follow the instructions to submit a PR. Done :-) |
||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.