forked from Exiled-Official/EXILED
-
Notifications
You must be signed in to change notification settings - Fork 107
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
Draft
VALERA771
wants to merge
8
commits into
ExMod-Team:master
Choose a base branch
from
VALERA771:validators-feat2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9718cad
feat: config validators
VALERA771 3728e8b
Update EXILED/Exiled.Loader/ConfigManager.cs
VALERA771 ae13e39
Update EXILED/Exiled.API/Interfaces/IValidator.cs
VALERA771 4797013
Update EXILED/Exiled.API/Features/Attributes/Validators/RangeAttribut…
VALERA771 b32cb4a
Squashed commit of the following:
Someone-193 fc726fc
Revert "Squashed commit of the following:"
Someone-193 909c579
Merge branch 'master' into pr/648
Someone-193 afd8661
I fix
Someone-193 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
EXILED/Exiled.API/Features/Attributes/ValidateChildrenAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="ValidateChildrenAttribute.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 | ||
| { | ||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Checks all properties in the target object for validators. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class ValidateChildrenAttribute : Attribute | ||
| { | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
EXILED/Exiled.API/Features/Attributes/Validators/AvailableValuesAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 other) => Values.Contains(other); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
EXILED/Exiled.API/Features/Attributes/Validators/CustomValidatorAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <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 System.Collections.Generic; | ||
|
|
||
| using Exiled.API.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Check a value with custom function. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] | ||
| public class CustomValidatorAttribute : Attribute, IValidator | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CustomValidatorAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="customFunctionType">The type of the custom check validator.</param> | ||
| /// <remarks> | ||
| /// The <see cref="Type"/> from customFunctionType must be a class inheriting IValidator with a parameterless constructor. | ||
| /// </remarks> | ||
| public CustomValidatorAttribute(Type customFunctionType) | ||
| { | ||
| if (!customFunctionType.IsClass || customFunctionType.IsAbstract || !customFunctionType.GetInterfaces().Contains(typeof(IValidator))) | ||
| throw new ArgumentException($"{nameof(customFunctionType)} must be a type inheriting IValidator!"); | ||
|
|
||
| CustomFunctionType = customFunctionType; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a <see cref="Dictionary{TKey,TValue}"/> from a type inheriting <see cref="IValidator"/>, to an instance of that class. | ||
| /// </summary> | ||
| public static Dictionary<Type, IValidator> ValidatorInstances { get; } = new(); | ||
|
|
||
| /// <summary> | ||
| /// Gets the type of the custom check validator. | ||
| /// </summary> | ||
| public Type CustomFunctionType { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object other) => ValidatorInstances.GetOrAdd(CustomFunctionType, () => (IValidator)Activator.CreateInstance(CustomFunctionType)).Check(other); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
EXILED/Exiled.API/Features/Attributes/Validators/GreaterOrEqualAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <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> | ||
| /// <remarks>value must be able to convert to your target type via <see cref="Convert.ChangeType(object, Type)"/>.</remarks> | ||
| public GreaterOrEqualAttribute(object value) | ||
| { | ||
| Value = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the minimum value. | ||
| /// </summary> | ||
| public object Value { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object other) => Convert.ChangeType(Value, other.GetType()) is IComparable min && min.CompareTo(other) <= 0; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
EXILED/Exiled.API/Features/Attributes/Validators/GreaterThanAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <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> | ||
| /// <remarks>value must be able to convert to your target type via <see cref="Convert.ChangeType(object, Type)"/>.</remarks> | ||
| public GreaterThanAttribute(object value) | ||
| { | ||
| Value = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the minimum value. | ||
| /// </summary> | ||
| public object Value { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object other) => Convert.ChangeType(Value, other.GetType()) is IComparable min && min.CompareTo(other) < 0; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
EXILED/Exiled.API/Features/Attributes/Validators/LessOrEqualAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <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> | ||
| /// <remarks>value must be able to convert to your target type via <see cref="Convert.ChangeType(object, Type)"/>.</remarks> | ||
| public LessOrEqualAttribute(object value) | ||
| { | ||
| Value = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the maximum value. | ||
| /// </summary> | ||
| public object Value { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object other) => Convert.ChangeType(Value, other.GetType()) is IComparable max && max.CompareTo(other) >= 0; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
EXILED/Exiled.API/Features/Attributes/Validators/LessThanAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <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> | ||
| /// <remarks>value must be able to convert to your target type via <see cref="Convert.ChangeType(object, Type)"/>.</remarks> | ||
| public LessThanAttribute(object value) | ||
| { | ||
| Value = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the maximum value. | ||
| /// </summary> | ||
| public object Value { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object other) => Convert.ChangeType(Value, other.GetType()) is IComparable max && max.CompareTo(other) > 0; | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
EXILED/Exiled.API/Features/Attributes/Validators/NonNegativeAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <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; | ||
|
|
||
| using Exiled.API.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Checks if value is 0 or greater. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class NonNegativeAttribute : Attribute, IValidator | ||
| { | ||
| /// <inheritdoc/> | ||
| public bool Check(object other) => Convert.ToDecimal(other) >= 0; | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
EXILED/Exiled.API/Features/Attributes/Validators/NonPositiveAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <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; | ||
|
|
||
| using Exiled.API.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Check if value is 0 or less. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class NonPositiveAttribute : Attribute, IValidator | ||
| { | ||
| /// <inheritdoc/> | ||
| public bool Check(object other) => Convert.ToDecimal(other) <= 0; | ||
| } | ||
Someone-193 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
57 changes: 57 additions & 0 deletions
57
EXILED/Exiled.API/Features/Attributes/Validators/RangeAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 an <see cref="IComparable"/> is inside a specific range. | ||
| /// </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> | ||
| /// <remarks>min and max must inherit <see cref="IComparable"/>.</remarks> | ||
| public RangeAttribute(object min, object max, bool inclusive = false) | ||
| { | ||
| Min = min; | ||
| Max = max; | ||
| Inclusive = inclusive; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the maximum value. | ||
| /// </summary> | ||
| public object Max { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the minimum value. | ||
| /// </summary> | ||
| public object Min { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets a value indicating whether check is inclusive. | ||
| /// </summary> | ||
| public bool Inclusive { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Check(object other) => | ||
| Convert.ChangeType(Min, other.GetType()) is IComparable min && | ||
| Convert.ChangeType(Max, other.GetType()) is IComparable max && | ||
| (Inclusive | ||
| ? min.CompareTo(other) <= 0 && max.CompareTo(other) >= 0 | ||
| : min.CompareTo(other) < 0 && max.CompareTo(other) > 0); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.