-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestValidation.cs
109 lines (103 loc) · 4.44 KB
/
TestValidation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System.Collections.Generic;
using Friflo.Json.Fliox.Schema;
using Friflo.Json.Fliox.Schema.Language;
using NUnit.Framework;
using static NUnit.Framework.Assert;
namespace SchemaValidation
{
public static class TestValidation
{
// --- primitive types
[Test]
public static void TestValidatePrimitives() {
{
var success = JsonValidator.Validate("1", typeof(int), out var error);
IsTrue(success);
} {
var success = JsonValidator.Validate("true", typeof(bool), out var error);
IsTrue(success);
} {
var success = JsonValidator.Validate("1.23", typeof(double), out var error);
IsTrue(success);
} {
var success = JsonValidator.Validate("\"abc\"", typeof(string), out var error);
IsTrue(success);
} {
var success = JsonValidator.Validate("\"xyz\"", typeof(int), out var error);
IsFalse(success);
AreEqual("Incorrect type. was: 'xyz', expect: int32 (root), pos: 5", error);
}
}
// --- array types
[Test]
public static void TestValidateArray() {
{
var success = JsonValidator.Validate("[1]", typeof(int[]), out var error);
IsTrue(success);
} {
var success = JsonValidator.Validate("[1,2,3]", typeof(List<int>), out var error);
IsTrue(success);
}
}
// --- optional class fields
[Test]
public static void TestValidateOptionalFields() {
{
var json = "{}";
var success = JsonValidator.Validate(json, typeof(OptionalFields), out var error);
IsTrue(success);
} {
var json = "{\"age\":42,\"name\":\"Peter\",\"gender\":\"male\",\"intArray\":[1]}";
var success = JsonValidator.Validate(json, typeof(OptionalFields), out var error);
IsTrue(success);
} {
var json = "{\"a\":1}";
var success = JsonValidator.Validate(json, typeof(OptionalFields), out var error);
IsFalse(success);
AreEqual("Unknown property: 'a' at OptionalFields > a, pos: 6", error);
} {
var json = "{\"age\":true}";
var success = JsonValidator.Validate(json, typeof(OptionalFields), out var error);
IsFalse(success);
AreEqual("Incorrect type. was: true, expect: int32 at OptionalFields > age, pos: 11", error);
}
}
// --- required class fields
[Test]
public static void TestValidateRequiredFields() {
{
var json = "{\"age\":42,\"name\":\"Peter\",\"gender\":\"male\",\"intArray\":[1]}";
var success = JsonValidator.Validate(json, typeof(RequiredFields), out var error);
IsTrue(success);
} {
var json = "{}";
var success = JsonValidator.Validate(json, typeof(RequiredFields), out var error);
IsFalse(success);
AreEqual("Missing required fields: [age, name, gender, intArray] at RequiredFields > (root), pos: 2", error);
}
}
// --- polymorph classes
[Test]
public static void TestValidatePolymorphType() {
{
var json = "{\"vehicleType\":\"car\",\"seatCount\":1}";
var success = JsonValidator.Validate(json, typeof(Vehicle), out var error);
IsTrue(success);
} {
var json = "{}";
var success = JsonValidator.Validate(json, typeof(Vehicle), out var error);
IsFalse(success);
AreEqual("Expect discriminator as first member. was: ObjectEnd, expect: 'vehicleType' at Vehicle > (root), pos: 2", error);
}
}
/// Generate: C#, GraphQL, HTML, JSON Schema, Kotlin, Markdown and Typescript in folder: ./schema
[Test]
public static void GenerateSchemaModels() {
var schemaModels = SchemaModel.GenerateSchemaModels(typeof(RequiredFields));
foreach (var schemaModel in schemaModels) {
var folder = $"./schema/{schemaModel.type}";
schemaModel.WriteFiles(folder);
}
}
}
}