-
Notifications
You must be signed in to change notification settings - Fork 104
feat: config validators #648
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
base: dev
Are you sure you want to change the base?
Changes from 1 commit
9718cad
3728e8b
ae13e39
4797013
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,37 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="AvailableValuesAttribute.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features.Attributes.Validators | ||
| { | ||
| using System; | ||
|
|
||
| using Exiled.API.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Checks if value is in list of available values. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class AvailableValuesAttribute : Attribute, IValidator | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AvailableValuesAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="values"><inheritdoc cref="Values"/></param> | ||
| public AvailableValuesAttribute(params object[] values) | ||
| { | ||
| Values = values; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the array of possible values. | ||
| /// </summary> | ||
| public object[] Values { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object value) => Values.Contains(value); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="CustomValidatorAttribute.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features.Attributes.Validators | ||
| { | ||
| using System; | ||
|
|
||
| using Exiled.API.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Check a value with custom function. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class CustomValidatorAttribute : Attribute, IValidator | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CustomValidatorAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="customFunction"><inheritdoc cref="CustomFunction"/></param> | ||
| public CustomValidatorAttribute(Func<object, bool> customFunction) | ||
| { | ||
| CustomFunction = customFunction; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the custom check function. | ||
| /// </summary> | ||
| public Func<object, bool> CustomFunction { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object value) => CustomFunction(value); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="GreaterOrEqualAttribute.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features.Attributes.Validators | ||
| { | ||
| using System; | ||
|
|
||
| using Exiled.API.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Checks if value greater or equal. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class GreaterOrEqualAttribute : Attribute, IValidator | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GreaterOrEqualAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="value"><inheritdoc cref="Value"/></param> | ||
| public GreaterOrEqualAttribute(IComparable value) | ||
| { | ||
| Value = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the minimum value. | ||
| /// </summary> | ||
| public IComparable Value { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object value) | ||
| { | ||
| return Value.CompareTo(value) >= 0; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="GreaterThanAttribute.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features.Attributes.Validators | ||
| { | ||
| using System; | ||
|
|
||
| using Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Check if value is greater. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class GreaterThanAttribute : Attribute, IValidator | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GreaterThanAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="value"><inheritdoc cref="Value"/></param> | ||
| public GreaterThanAttribute(IComparable value) | ||
| { | ||
| Value = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the minimum value. | ||
| /// </summary> | ||
| public IComparable Value { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object value) | ||
| { | ||
| return Value.CompareTo(value) >= 1; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="LessOrEqualAttribute.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features.Attributes.Validators | ||
| { | ||
| using System; | ||
|
|
||
| using Exiled.API.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Checks if value less or equal. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class LessOrEqualAttribute : Attribute, IValidator | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="LessOrEqualAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="value"><inheritdoc cref="Value"/></param> | ||
| public LessOrEqualAttribute(IComparable value) | ||
| { | ||
| Value = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the minimum value. | ||
| /// </summary> | ||
| public IComparable Value { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object value) | ||
| { | ||
| return Value.CompareTo(value) <= 0; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="LessThanAttribute.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features.Attributes.Validators | ||
| { | ||
| using System; | ||
|
|
||
| using Exiled.API.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Checks if value is less. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class LessThanAttribute : Attribute, IValidator | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="LessThanAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="value"><inheritdoc cref="Value"/></param> | ||
| public LessThanAttribute(IComparable value) | ||
| { | ||
| Value = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the minimum value. | ||
|
Collaborator
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. Wouldn't this be a maximum value if this attribute checks if a given value in a config is less than the value provided to the attribute? |
||
| /// </summary> | ||
| public IComparable Value { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object value) | ||
| { | ||
| return Value.CompareTo(value) <= -1; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="NonNegativeAttribute.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features.Attributes.Validators | ||
| { | ||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Checks if value is 0 or greater. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class NonNegativeAttribute : GreaterOrEqualAttribute | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="NonNegativeAttribute"/> class. | ||
| /// </summary> | ||
| public NonNegativeAttribute() | ||
| : base(0) | ||
| { | ||
| } | ||
| } | ||
|
Comment on lines
+12
to
+25
Collaborator
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. Maybe call this PositiveAttribute or IsPositiveAttribute? NonNegative is a bit convoluted |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="NonPositiveAttribute.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features.Attributes.Validators | ||
| { | ||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Check if value is 0 or less. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class NonPositiveAttribute : LessOrEqualAttribute | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="NonPositiveAttribute"/> class. | ||
| /// </summary> | ||
| public NonPositiveAttribute() | ||
| : base(0) | ||
| { | ||
| } | ||
| } | ||
|
Comment on lines
+12
to
+25
Collaborator
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. Maybe call this NegativeAttribute or IsNegativeAttribute? NonPositive is a bit convoluted |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="RangeAttribute.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Features.Attributes.Validators | ||
| { | ||
| using System; | ||
|
|
||
| using Exiled.API.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Check if <see cref="IComparable"/> is inside a specific range. | ||
VALERA771 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class RangeAttribute : Attribute, IValidator | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="RangeAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="min"><inheritdoc cref="Min"/></param> | ||
| /// <param name="max"><inheritdoc cref="Max"/></param> | ||
| /// <param name="inclusive"><inheritdoc cref="Inclusive"/></param> | ||
| public RangeAttribute(IComparable min, IComparable max, bool inclusive = false) | ||
| { | ||
| Min = min; | ||
| Max = max; | ||
| Inclusive = inclusive; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the minimum value. | ||
| /// </summary> | ||
| public IComparable Max { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the maximum value. | ||
| /// </summary> | ||
| public IComparable Min { get; } | ||
|
Comment on lines
+33
to
+41
Collaborator
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. These docs are flipped? |
||
|
|
||
| /// <summary> | ||
| /// Gets a value indicating whether check is inclusive. | ||
| /// </summary> | ||
| public bool Inclusive { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object value) | ||
| { | ||
| int minResult = Inclusive ? 0 : -1; | ||
| int maxResult = Inclusive ? 0 : 1; | ||
|
|
||
| return Max.CompareTo(value) <= minResult && Min.CompareTo(value) >= maxResult; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="IValidator.cs" company="ExMod Team"> | ||
| // Copyright (c) ExMod Team. All rights reserved. | ||
| // Licensed under the CC BY-SA 3.0 license. | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| namespace Exiled.API.Interfaces | ||
| { | ||
| /// <summary> | ||
| /// Interface for all validations attributes. | ||
| /// </summary> | ||
| public interface IValidator | ||
| { | ||
| /// <summary> | ||
| /// Checks if <paramref name="value"/> is satisfying condition. | ||
VALERA771 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| /// <param name="value">Value to check.</param> | ||
| /// <returns>Whether the value has passed check.</returns> | ||
| public bool Check(object value); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be a maximum value if this attribute checks if a given value in a config is less than the value provided to the attribute?