Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
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.
Copy link
Collaborator

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?

/// </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.
Copy link
Collaborator

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?

/// </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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this NegativeAttribute or IsNegativeAttribute? NonPositive is a bit convoluted

}
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 <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>
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
}
}
22 changes: 22 additions & 0 deletions EXILED/Exiled.API/Interfaces/IValidator.cs
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.
/// </summary>
/// <param name="value">Value to check.</param>
/// <returns>Whether the value has passed check.</returns>
public bool Check(object value);
}
}
6 changes: 6 additions & 0 deletions EXILED/Exiled.Loader/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,11 @@ public sealed class Config : IConfig
/// </summary>
[Description("Indicates whether Exiled should auto-update itself as soon as a new release is available.")]
public bool EnableAutoUpdates { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether config validator should check all properties inside config values' types.
/// </summary>
[Description("Indicating whether config validator should check all properties inside config values' types.")]
public bool EnableDeepValidation { get; set; } = true;
}
}
Loading
Loading