Skip to content

Commit 6a40d7e

Browse files
Improve range checking
1 parent c0f827e commit 6a40d7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+429
-492
lines changed

C Sharp/Object Validator Test/Object Validator Test.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
1616
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
1717
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
18-
<PackageReference Include="Quicksilver0218.ObjectValidator" Version="2.0.0" />
18+
<PackageReference Include="Quicksilver0218.ObjectValidator" Version="2.0.1" />
1919
<None Include="rules.json">
2020
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2121
</None>

C Sharp/Object Validator/Object Validator.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<PackageId>Quicksilver0218.ObjectValidator</PackageId>
9-
<PackageVersion>2.0.0</PackageVersion>
9+
<PackageVersion>2.0.1</PackageVersion>
1010
<Authors>Quicksilver0218</Authors>
1111
<Copyright>Copyright (c) Quicksilver0218 2024</Copyright>
1212
<Description>

C Sharp/Object Validator/Runtime/And.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ class And(bool reversed, string? fieldExpression, Condition[] conditions) : Cond
33
{
44
protected readonly Condition[] conditions = conditions;
55

6-
protected override bool IsFulfilledBy(object? field, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
6+
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
77
{
88
HashSet<string> newPassedFields = [];
99
foreach (Condition condition in conditions)
10-
if (!condition.Check(field, fullFieldExpression, newPassedFields, failedFields))
10+
if (!condition.Check(value, fullFieldExpression, newPassedFields, failedFields))
1111
return false;
1212
passedFields.UnionWith(newPassedFields);
1313
return true;
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
namespace Quicksilver.ObjectValidator.Runtime;
22
class Blank(bool reversed, string? fieldExpression) : Condition(reversed, fieldExpression)
33
{
4-
protected override bool IsFulfilledBy(object? field, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
4+
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
55
{
6-
if (field == null)
6+
if (value == null)
77
throw new Exception("Null values are not supported for 'blank'.");
8-
if (field is string s)
8+
if (value is string s)
99
return string.IsNullOrWhiteSpace(s);
10-
throw new Exception("Unsupported type for 'blank': " + field.GetType());
10+
throw new Exception("Unsupported type for 'blank': " + value.GetType());
1111
}
1212
}

C Sharp/Object Validator/Runtime/Bytes.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ class Bytes(bool reversed, string? fieldExpression, string range) : Condition(re
55
{
66
private readonly string range = range;
77

8-
protected override bool IsFulfilledBy(object? field, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
8+
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
99
{
10-
if (field == null)
10+
if (value == null)
1111
throw new Exception("Null values are not supported for 'bytes'.");
12-
if (field is string s)
12+
if (value is string s)
1313
return Utils.InRange(Encoding.UTF8.GetByteCount(s), range);
14-
throw new Exception("Unsupported type for 'bytes': " + field.GetType());
14+
throw new Exception("Unsupported type for 'bytes': " + value.GetType());
1515
}
1616
}

C Sharp/Object Validator/Runtime/Condition.cs

+35-35
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,45 @@ abstract class Condition(bool reversed, string? fieldExpression)
66
protected readonly bool reversed = reversed;
77
protected readonly string? fieldExpression = fieldExpression;
88

9-
private static void HandleField(object o, string field, List<object?> fields) {
10-
fields.Add(o.GetType().GetField(field)!.GetValue(o));
9+
private static void HandleField(object obj, string field, List<object?> values) {
10+
values.Add(obj.GetType().GetField(field)!.GetValue(obj));
1111
}
1212

13-
private static void HandleIndex(IList list, int index, List<object?> fields) {
14-
fields.Add(list[index]);
13+
private static void HandleIndex(IList list, int index, List<object?> values) {
14+
values.Add(list[index]);
1515
}
1616

17-
private static void HandleKey(IDictionary map, string key, List<object?> fields) {
18-
fields.Add(map[key]);
17+
private static void HandleKey(IDictionary map, string key, List<object?> values) {
18+
values.Add(map[key]);
1919
}
2020

21-
private static void HandleFields(List<object?> fields, string name, List<object?> newFields) {
22-
foreach (object? o in fields)
23-
if (o == null)
24-
newFields.Add(null);
25-
else if (o is IDictionary d)
26-
HandleKey(d, name, newFields);
27-
else if (o is IList l && int.TryParse(name, out int index))
28-
HandleIndex(l, index, newFields);
21+
private static void HandleValues(List<object?> values, string name, List<object?> newValues) {
22+
foreach (object? value in values)
23+
if (value == null)
24+
newValues.Add(null);
25+
else if (value is IDictionary d)
26+
HandleKey(d, name, newValues);
27+
else if (value is IList l && int.TryParse(name, out int index))
28+
HandleIndex(l, index, newValues);
2929
else
30-
HandleField(o, name, newFields);
30+
HandleField(value, name, newValues);
3131
}
3232

3333
internal bool Check(object? root, string? rootFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields) {
34-
List<object?> fields = [root];
34+
List<object?> values = [root];
3535
if (fieldExpression != null) {
3636
if (root != null) {
3737
string fullName = "";
3838
foreach (string name in fieldExpression.Split('.')) {
39-
List<object?> newFields = [];
39+
List<object?> newValues = [];
4040
fullName += name;
4141
if (fullName == "*")
42-
foreach (object? o in fields)
42+
foreach (object? o in values)
4343
if (o == null)
44-
newFields.Add(null);
44+
newValues.Add(null);
4545
else if (o is IEnumerable e)
4646
foreach (object item in e)
47-
newFields.Add(item);
47+
newValues.Add(item);
4848
else
4949
throw new Exception("Unsupported type for iteration: " + o.GetType());
5050
else if (name.Length >= 3 && name[^3..^1] == "//")
@@ -56,35 +56,35 @@ internal bool Check(object? root, string? rootFieldExpression, HashSet<string> p
5656
fullName += ".";
5757
continue;
5858
case 'F':
59-
foreach (object? o in fields)
59+
foreach (object? o in values)
6060
if (o == null)
61-
newFields.Add(null);
61+
newValues.Add(null);
6262
else
63-
HandleField(o, fullName, newFields);
63+
HandleField(o, fullName, newValues);
6464
break;
6565
case 'I':
66-
foreach (object? o in fields)
66+
foreach (object? o in values)
6767
if (o == null)
68-
newFields.Add(null);
68+
newValues.Add(null);
6969
else
70-
HandleIndex((IList)o, int.Parse(fullName), newFields);
70+
HandleIndex((IList)o, int.Parse(fullName), newValues);
7171
break;
7272
case 'K':
73-
foreach (object? o in fields)
73+
foreach (object? o in values)
7474
if (o == null)
75-
newFields.Add(null);
75+
newValues.Add(null);
7676
else
77-
HandleKey((IDictionary)o, fullName, newFields);
77+
HandleKey((IDictionary)o, fullName, newValues);
7878
break;
7979
case '*':
80-
HandleFields(fields, fullName + "*", newFields);
80+
HandleValues(values, fullName + "*", newValues);
8181
break;
8282
default:
8383
throw new Exception("Unsupported suffix: " + name[^1]);
8484
}
8585
} else
86-
HandleFields(fields, name, newFields);
87-
fields = newFields;
86+
HandleValues(values, name, newValues);
87+
values = newValues;
8888
fullName = "";
8989
}
9090
}
@@ -94,8 +94,8 @@ internal bool Check(object? root, string? rootFieldExpression, HashSet<string> p
9494
rootFieldExpression = fieldExpression;
9595
}
9696
HashSet<string> childPassedFields = [], childFailedFields = [];
97-
foreach (object? field in fields)
98-
if (IsFulfilledBy(field, rootFieldExpression, childPassedFields, childFailedFields) == reversed) {
97+
foreach (object? value in values)
98+
if (IsFulfilledBy(value, rootFieldExpression, childPassedFields, childFailedFields) == reversed) {
9999
if (rootFieldExpression != null)
100100
failedFields.Add(rootFieldExpression);
101101
if (reversed)
@@ -113,5 +113,5 @@ internal bool Check(object? root, string? rootFieldExpression, HashSet<string> p
113113
return true;
114114
}
115115

116-
protected abstract bool IsFulfilledBy(object? field, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields);
116+
protected abstract bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields);
117117
}

C Sharp/Object Validator/Runtime/Contains.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ class Contains(bool reversed, string? fieldExpression, string? arg) : Condition(
55
{
66
private readonly string? arg = arg;
77

8-
protected override bool IsFulfilledBy(object? field, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
8+
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
99
{
10-
if (field == null)
10+
if (value == null)
1111
throw new Exception("Null values are not supported for 'contains'.");
12-
if (field is string s)
12+
if (value is string s)
1313
return s.Contains(arg!);
14-
if (field is IEnumerable e) {
14+
if (value is IEnumerable e) {
1515
foreach (object o in e)
1616
if (o == null) {
1717
if (arg == null)
@@ -20,6 +20,6 @@ protected override bool IsFulfilledBy(object? field, string? fullFieldExpression
2020
return true;
2121
return false;
2222
}
23-
throw new Exception("Unsupported type for 'contains': " + field.GetType());
23+
throw new Exception("Unsupported type for 'contains': " + value.GetType());
2424
}
2525
}

C Sharp/Object Validator/Runtime/In.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ class In(bool reversed, string? fieldExpression, string?[] args) : Condition(rev
33
{
44
private readonly string?[] args = args;
55

6-
protected override bool IsFulfilledBy(object? field, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
6+
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
77
{
8-
if (field == null) {
8+
if (value == null) {
99
foreach (string? arg in args)
1010
if (arg == null)
1111
return true;
1212
return false;
1313
}
1414
foreach (string? arg in args)
15-
if (field.ToString() == arg)
15+
if (value.ToString() == arg)
1616
return true;
1717
return false;
1818
}

C Sharp/Object Validator/Runtime/Length.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ class Length(bool reversed, string? fieldExpression, string range) : Condition(r
55
{
66
private readonly string range = range;
77

8-
protected override bool IsFulfilledBy(object? field, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
8+
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
99
{
10-
if (field == null)
10+
if (value == null)
1111
throw new Exception("Null values are not supported for 'length'.");
12-
if (field is string s)
12+
if (value is string s)
1313
return Utils.InRange(s.Length, range);
14-
if (field is ICollection c)
14+
if (value is ICollection c)
1515
return Utils.InRange(c.Count, range);
16-
throw new Exception("Unsupported type for 'length': " + field.GetType());
16+
throw new Exception("Unsupported type for 'length': " + value.GetType());
1717
}
1818
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace Quicksilver.ObjectValidator.Runtime;
22
class Null(bool reversed, string? fieldExpression) : Condition(reversed, fieldExpression)
33
{
4-
protected override bool IsFulfilledBy(object? field, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
4+
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
55
{
6-
return field == null;
6+
return value == null;
77
}
88
}

C Sharp/Object Validator/Runtime/Or.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ class Or(bool reversed, string? fieldExpression, Condition[] conditions) : Condi
33
{
44
protected readonly Condition[] conditions = conditions;
55

6-
protected override bool IsFulfilledBy(object? field, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
6+
protected override bool IsFulfilledBy(object? value, string? fullFieldExpression, HashSet<string> passedFields, HashSet<string> failedFields)
77
{
88
HashSet<string> newFailedFields = [];
99
foreach (Condition condition in conditions)
10-
if (condition.Check(field, fullFieldExpression, passedFields, newFailedFields))
10+
if (condition.Check(value, fullFieldExpression, passedFields, newFailedFields))
1111
return true;
1212
failedFields.UnionWith(newFailedFields);
1313
return false;

0 commit comments

Comments
 (0)