mirror of https://github.com/Squidex/squidex.git
47 changed files with 470 additions and 186 deletions
@ -0,0 +1,47 @@ |
|||
// ==========================================================================
|
|||
// JsonField.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using Microsoft.OData.Edm; |
|||
using Newtonsoft.Json.Linq; |
|||
using NJsonSchema; |
|||
using Squidex.Core.Schemas.Validators; |
|||
|
|||
namespace Squidex.Core.Schemas |
|||
{ |
|||
public sealed class JsonField : Field<JsonFieldProperties> |
|||
{ |
|||
public JsonField(long id, string name, JsonFieldProperties properties) |
|||
: base(id, name, properties) |
|||
{ |
|||
} |
|||
|
|||
protected override IEnumerable<IValidator> CreateValidators() |
|||
{ |
|||
if (Properties.IsRequired) |
|||
{ |
|||
yield return new RequiredValidator(); |
|||
} |
|||
} |
|||
|
|||
protected override object ConvertValue(JToken value) |
|||
{ |
|||
return value; |
|||
} |
|||
|
|||
protected override void PrepareJsonSchema(JsonProperty jsonProperty) |
|||
{ |
|||
jsonProperty.Type = JsonObjectType.Object; |
|||
} |
|||
|
|||
protected override IEdmTypeReference CreateEdmType() |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
// ==========================================================================
|
|||
// JsonFieldProperties.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using Newtonsoft.Json.Linq; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Core.Schemas |
|||
{ |
|||
[TypeName("JsonField")] |
|||
public sealed class JsonFieldProperties : FieldProperties |
|||
{ |
|||
public override JToken GetDefaultValue() |
|||
{ |
|||
return JValue.CreateNull(); |
|||
} |
|||
|
|||
protected override IEnumerable<ValidationError> ValidateCore() |
|||
{ |
|||
yield break; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
// ==========================================================================
|
|||
// JsonFieldPropertiesDto.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using NJsonSchema.Annotations; |
|||
using Squidex.Core.Schemas; |
|||
using Squidex.Infrastructure.Reflection; |
|||
|
|||
namespace Squidex.Controllers.Api.Schemas.Models |
|||
{ |
|||
[JsonSchema("Json")] |
|||
public sealed class JsonFieldPropertiesDto : FieldPropertiesDto |
|||
{ |
|||
public override FieldProperties ToProperties() |
|||
{ |
|||
return SimpleMapper.Map(this, new JsonFieldProperties()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
<div [formGroup]="editForm"> |
|||
<span>Nothing to setup</span> |
|||
<div> |
|||
@ -0,0 +1,2 @@ |
|||
@import '_vars'; |
|||
@import '_mixins'; |
|||
@ -0,0 +1,24 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
import { Component, Input } from '@angular/core'; |
|||
import { FormGroup } from '@angular/forms'; |
|||
|
|||
import { JsonFieldPropertiesDto } from 'shared'; |
|||
|
|||
@Component({ |
|||
selector: 'sqx-json-ui', |
|||
styleUrls: ['json-ui.component.scss'], |
|||
templateUrl: 'json-ui.component.html' |
|||
}) |
|||
export class JsonUIComponent { |
|||
@Input() |
|||
public editForm: FormGroup; |
|||
|
|||
@Input() |
|||
public properties: JsonFieldPropertiesDto; |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
<div [formGroup]="editForm"> |
|||
<div class="form-group row"> |
|||
<label class="col col-3 col-form-checkbox-label" for="field-required">Required</label> |
|||
|
|||
<div class="col col-6"> |
|||
<input type="checkbox" class="form-check-input" id="field-required" formControlName="isRequired" /> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
@ -0,0 +1,10 @@ |
|||
@import '_vars'; |
|||
@import '_mixins'; |
|||
|
|||
.form-check-input { |
|||
margin: 0; |
|||
} |
|||
|
|||
.form-group { |
|||
margin-top: .5rem; |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
import { Component, Input } from '@angular/core'; |
|||
import { FormGroup } from '@angular/forms'; |
|||
|
|||
import { JsonFieldPropertiesDto } from 'shared'; |
|||
|
|||
@Component({ |
|||
selector: 'sqx-json-validation', |
|||
styleUrls: ['json-validation.component.scss'], |
|||
templateUrl: 'json-validation.component.html' |
|||
}) |
|||
export class JsonValidationComponent { |
|||
@Input() |
|||
public editForm: FormGroup; |
|||
|
|||
@Input() |
|||
public properties: JsonFieldPropertiesDto; |
|||
} |
|||
Binary file not shown.
|
Before Width: | Height: | Size: 49 KiB After Width: | Height: | Size: 49 KiB |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,63 @@ |
|||
// ==========================================================================
|
|||
// JsonFieldTests.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using FluentAssertions; |
|||
using Newtonsoft.Json.Linq; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Core.Schemas |
|||
{ |
|||
public class JsonFieldTests |
|||
{ |
|||
private readonly List<string> errors = new List<string>(); |
|||
|
|||
[Fact] |
|||
public void Should_instantiate_field() |
|||
{ |
|||
var sut = new JsonField(1, "my-json", new JsonFieldProperties()); |
|||
|
|||
Assert.Equal("my-json", sut.Name); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_clone_object() |
|||
{ |
|||
var sut = new JsonField(1, "my-json", new JsonFieldProperties()); |
|||
|
|||
Assert.NotEqual(sut, sut.Enable()); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_not_add_error_if_json_is_valid() |
|||
{ |
|||
var sut = new JsonField(1, "my-json", new JsonFieldProperties { Label = "My-Json" }); |
|||
|
|||
await sut.ValidateAsync(CreateValue(null), errors); |
|||
|
|||
Assert.Empty(errors); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_add_errors_if_datetime_is_required() |
|||
{ |
|||
var sut = new JsonField(1, "my-json", new JsonFieldProperties { Label = "My-Json", IsRequired = true }); |
|||
|
|||
await sut.ValidateAsync(CreateValue(null), errors); |
|||
|
|||
errors.ShouldBeEquivalentTo( |
|||
new[] { "My-Json is required" }); |
|||
} |
|||
|
|||
private static JValue CreateValue(JValue v) |
|||
{ |
|||
return v; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue