JsonStringValidator.cs
3.95 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace UniJSON
{
/// <summary>
/// http://json-schema.org/latest/json-schema-validation.html#string
/// </summary>
public class JsonStringValidator : IJsonSchemaValidator
{
/// <summary>
/// http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.3.1
/// </summary>
public int? MaxLength
{
get; set;
}
/// <summary>
/// http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.3.2
/// </summary>
public int? MinLength
{
get; set;
}
/// <summary>
/// http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.3.3
/// </summary>
public Regex Pattern
{
get; set;
}
public override int GetHashCode()
{
return 4;
}
public override bool Equals(object obj)
{
var rhs = obj as JsonStringValidator;
if (rhs == null) return false;
if (MaxLength != rhs.MaxLength) return false;
if (MinLength != rhs.MinLength) return false;
if (Pattern == null && rhs.Pattern == null)
{
}
else if (Pattern == null)
{
return false;
}
else if (rhs.Pattern == null)
{
return false;
}
else if (Pattern.ToString() != rhs.Pattern.ToString())
{
return false;
}
return true;
}
public void Merge(IJsonSchemaValidator obj)
{
var rhs = obj as JsonStringValidator;
if (rhs == null)
{
throw new ArgumentException();
}
MaxLength = rhs.MaxLength;
MinLength = rhs.MinLength;
Pattern = rhs.Pattern;
}
public bool FromJsonSchema(IFileSystemAccessor fs, string key, ListTreeNode<JsonValue> value)
{
switch (key)
{
case "maxLength":
MaxLength = value.GetInt32();
return true;
case "minLength":
MinLength = value.GetInt32();
return true;
case "pattern":
Pattern = new Regex(value.GetString().Replace("\\\\", "\\"));
return true;
}
return false;
}
public void ToJsonScheama(IFormatter f)
{
f.Key("type"); f.Value("string");
}
public JsonSchemaValidationException Validate<T>(JsonSchemaValidationContext c, T o)
{
if (o == null)
{
return new JsonSchemaValidationException(c, "null");
}
var value = o as string;
if (MinLength.HasValue && value.Length < MinLength)
{
return new JsonSchemaValidationException(c, string.Format("minlength: {0}<{1}", value.Length, MinLength.Value));
}
if (MaxLength.HasValue && value.Length > MaxLength)
{
return new JsonSchemaValidationException(c, string.Format("maxlength: {0}>{1}", value.Length, MaxLength.Value));
}
if (Pattern != null && !Pattern.IsMatch(value))
{
return new JsonSchemaValidationException(c, string.Format("pattern: {0} not match {1}", Pattern, value));
}
return null;
}
public void Serialize<T>(IFormatter f, JsonSchemaValidationContext c, T o)
{
f.Value(GenericCast<T, string>.Cast(o));
}
public void Deserialize<T, U>(ListTreeNode<T> src, ref U dst)
where T: IListTreeItem, IValue<T>
{
dst = GenericCast<string, U>.Cast(src.GetString());
}
}
}