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.4 KiB
75 lines
2.4 KiB
// ==========================================================================
|
|
// DateTimeField.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.OData.Edm;
|
|
using Newtonsoft.Json.Linq;
|
|
using NJsonSchema;
|
|
using NodaTime;
|
|
using NodaTime.Text;
|
|
using Squidex.Domain.Apps.Core.Schemas.Validators;
|
|
using Squidex.Infrastructure;
|
|
|
|
namespace Squidex.Domain.Apps.Core.Schemas
|
|
{
|
|
public sealed class DateTimeField : Field<DateTimeFieldProperties>
|
|
{
|
|
public DateTimeField(long id, string name, Partitioning partitioning)
|
|
: this(id, name, partitioning, new DateTimeFieldProperties())
|
|
{
|
|
}
|
|
|
|
public DateTimeField(long id, string name, Partitioning partitioning, DateTimeFieldProperties properties)
|
|
: base(id, name, partitioning, properties)
|
|
{
|
|
}
|
|
|
|
protected override IEnumerable<IValidator> CreateValidators()
|
|
{
|
|
if (Properties.IsRequired)
|
|
{
|
|
yield return new RequiredValidator();
|
|
}
|
|
|
|
if (Properties.MinValue.HasValue || Properties.MaxValue.HasValue)
|
|
{
|
|
yield return new RangeValidator<Instant>(Properties.MinValue, Properties.MaxValue);
|
|
}
|
|
}
|
|
|
|
public override object ConvertValue(JToken value)
|
|
{
|
|
if (value.Type == JTokenType.String)
|
|
{
|
|
var parseResult = InstantPattern.General.Parse(value.ToString());
|
|
|
|
if (!parseResult.Success)
|
|
{
|
|
throw parseResult.Exception;
|
|
}
|
|
|
|
return parseResult.Value;
|
|
}
|
|
|
|
throw new InvalidCastException("Invalid json type, expected string.");
|
|
}
|
|
|
|
protected override void PrepareJsonSchema(JsonProperty jsonProperty, Func<string, JsonSchema4, JsonSchema4> schemaResolver)
|
|
{
|
|
jsonProperty.Type = JsonObjectType.String;
|
|
|
|
jsonProperty.Format = JsonFormatStrings.DateTime;
|
|
}
|
|
|
|
protected override IEdmTypeReference CreateEdmType()
|
|
{
|
|
return EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.DateTimeOffset, !Properties.IsRequired);
|
|
}
|
|
}
|
|
}
|
|
|