mirror of https://github.com/Squidex/squidex.git
6 changed files with 166 additions and 107 deletions
@ -0,0 +1,87 @@ |
|||
// ==========================================================================
|
|||
// FieldPropertiesDtoFactory.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Linq; |
|||
using Squidex.Domain.Apps.Core.Schemas; |
|||
using Squidex.Infrastructure.Reflection; |
|||
|
|||
namespace Squidex.Controllers.Api.Schemas.Models.Converters |
|||
{ |
|||
public class FieldPropertiesDtoFactory : IFieldPropertiesVisitor<FieldPropertiesDto> |
|||
{ |
|||
private static readonly FieldPropertiesDtoFactory Instance = new FieldPropertiesDtoFactory(); |
|||
|
|||
private FieldPropertiesDtoFactory() |
|||
{ |
|||
} |
|||
|
|||
public static FieldPropertiesDto Create(FieldProperties properties) |
|||
{ |
|||
return properties.Accept(Instance); |
|||
} |
|||
|
|||
public FieldPropertiesDto Visit(AssetsFieldProperties properties) |
|||
{ |
|||
return SimpleMapper.Map(properties, new AssetsFieldPropertiesDto()); |
|||
} |
|||
|
|||
public FieldPropertiesDto Visit(BooleanFieldProperties properties) |
|||
{ |
|||
return SimpleMapper.Map(properties, new BooleanFieldPropertiesDto()); |
|||
} |
|||
|
|||
public FieldPropertiesDto Visit(DateTimeFieldProperties properties) |
|||
{ |
|||
return SimpleMapper.Map(properties, new DateTimeFieldPropertiesDto()); |
|||
} |
|||
|
|||
public FieldPropertiesDto Visit(GeolocationFieldProperties properties) |
|||
{ |
|||
return SimpleMapper.Map(properties, new GeolocationFieldPropertiesDto()); |
|||
} |
|||
|
|||
public FieldPropertiesDto Visit(JsonFieldProperties properties) |
|||
{ |
|||
return SimpleMapper.Map(properties, new JsonFieldPropertiesDto()); |
|||
} |
|||
|
|||
public FieldPropertiesDto Visit(ReferencesFieldProperties properties) |
|||
{ |
|||
return SimpleMapper.Map(properties, new ReferencesFieldPropertiesDto()); |
|||
} |
|||
|
|||
public FieldPropertiesDto Visit(TagsFieldProperties properties) |
|||
{ |
|||
return SimpleMapper.Map(properties, new TagsFieldPropertiesDto()); |
|||
} |
|||
|
|||
public FieldPropertiesDto Visit(NumberFieldProperties properties) |
|||
{ |
|||
var result = SimpleMapper.Map(properties, new NumberFieldPropertiesDto()); |
|||
|
|||
if (properties.AllowedValues != null) |
|||
{ |
|||
result.AllowedValues = properties.AllowedValues.ToArray(); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
public FieldPropertiesDto Visit(StringFieldProperties properties) |
|||
{ |
|||
var result = SimpleMapper.Map(properties, new StringFieldPropertiesDto()); |
|||
|
|||
if (properties.AllowedValues != null) |
|||
{ |
|||
result.AllowedValues = properties.AllowedValues.ToArray(); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// ==========================================================================
|
|||
// TagsFieldPropertiesDto.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using NJsonSchema.Annotations; |
|||
using Squidex.Domain.Apps.Core.Schemas; |
|||
using Squidex.Infrastructure.Reflection; |
|||
|
|||
namespace Squidex.Controllers.Api.Schemas.Models |
|||
{ |
|||
[JsonSchema("Tags")] |
|||
public sealed class TagsFieldPropertiesDto : FieldPropertiesDto |
|||
{ |
|||
/// <summary>
|
|||
/// The minimum allowed items for the field value.
|
|||
/// </summary>
|
|||
public int? MinItems { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// The maximum allowed items for the field value.
|
|||
/// </summary>
|
|||
public int? MaxItems { get; set; } |
|||
|
|||
public override FieldProperties ToProperties() |
|||
{ |
|||
return SimpleMapper.Map(this, new TagsFieldProperties()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
// ==========================================================================
|
|||
// TagsFieldPropertiesTests.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using FluentAssertions; |
|||
using Squidex.Domain.Apps.Core.Schemas; |
|||
using Squidex.Infrastructure; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Write.Schemas.Guards.FieldProperties |
|||
{ |
|||
public class TagsFieldPropertiesTests |
|||
{ |
|||
[Fact] |
|||
public void Should_add_error_if_min_greater_than_max() |
|||
{ |
|||
var sut = new TagsFieldProperties { MinItems = 10, MaxItems = 5 }; |
|||
|
|||
var errors = FieldPropertiesValidator.Validate(sut).ToList(); |
|||
|
|||
errors.ShouldBeEquivalentTo( |
|||
new List<ValidationError> |
|||
{ |
|||
new ValidationError("Max items must be greater than min items.", "MinItems", "MaxItems") |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue