mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.1 KiB
75 lines
2.1 KiB
// ==========================================================================
|
|
// NumberField.cs
|
|
// PinkParrot Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) PinkParrot Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using PinkParrot.Infrastructure;
|
|
using PinkParrot.Infrastructure.Tasks;
|
|
|
|
namespace PinkParrot.Core.Schemas
|
|
{
|
|
public sealed class NumberField : Field<NumberFieldProperties>
|
|
{
|
|
public double? MaxValue
|
|
{
|
|
get { return Properties.MaxValue; }
|
|
}
|
|
|
|
public double? MinValue
|
|
{
|
|
get { return Properties.MinValue; }
|
|
}
|
|
|
|
public double[] AllowedValues
|
|
{
|
|
get { return Properties.AllowedValues; }
|
|
}
|
|
|
|
public NumberField(long id, string name, NumberFieldProperties properties)
|
|
: base(id, name, properties)
|
|
{
|
|
}
|
|
|
|
protected override Task ValidateCoreAsync(PropertyValue property, ICollection<string> errors)
|
|
{
|
|
try
|
|
{
|
|
var value = property.ToDouble(CultureInfo.InvariantCulture);
|
|
|
|
if (MinValue.HasValue && value < MinValue.Value)
|
|
{
|
|
errors.Add($"Must be greater than {MinValue}");
|
|
}
|
|
|
|
if (MaxValue.HasValue && value > MaxValue.Value)
|
|
{
|
|
errors.Add($"Must be less than {MaxValue}");
|
|
}
|
|
|
|
if (AllowedValues != null && !AllowedValues.Contains(value))
|
|
{
|
|
errors.Add($"Can only be one of the following value: {string.Join(", ", AllowedValues)}");
|
|
}
|
|
}
|
|
catch (InvalidCastException)
|
|
{
|
|
errors.Add("Value is not a valid number");
|
|
}
|
|
|
|
return TaskHelper.Done;
|
|
}
|
|
|
|
protected override Field Clone()
|
|
{
|
|
return (Field)MemberwiseClone();
|
|
}
|
|
}
|
|
}
|
|
|