Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions EXILED/Exiled.API/Features/Attributes/ValidateChildrenAttribute.cs
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
{
}
}
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);
}
}
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);
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
57 changes: 57 additions & 0 deletions EXILED/Exiled.API/Features/Attributes/Validators/RangeAttribute.cs
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);
}
}
Loading
Loading