|
| 1 | +import 'package:vine/src/contracts/rule.dart'; |
1 | 2 | import 'package:vine/src/contracts/schema.dart'; |
2 | 3 | import 'package:vine/src/contracts/vine.dart'; |
3 | 4 | import 'package:vine/src/field_pool.dart'; |
4 | 5 | import 'package:vine/src/rule_parser.dart'; |
5 | 6 |
|
6 | | -void arrayRuleHandler(VineValidationContext ctx, FieldContext field, VineSchema schema) { |
7 | | - final copy = field.customKeys; |
| 7 | +final class VineArrayRule implements VineRule { |
| 8 | + final VineSchema schema; |
8 | 9 |
|
9 | | - if (field.value case List values) { |
10 | | - final currentSchema = schema as RuleParser; |
11 | | - final copyRules = currentSchema.rules.toList(); |
| 10 | + const VineArrayRule(this.schema); |
12 | 11 |
|
13 | | - for (int i = 0; i < values.length; i++) { |
14 | | - final currentField = FieldPool.acquire(field.name, values[i]); |
| 12 | + @override |
| 13 | + void handle(VineValidationContext ctx, FieldContext field) { |
| 14 | + final copy = field.customKeys; |
15 | 15 |
|
16 | | - currentSchema.rules.clear(); |
17 | | - currentSchema.rules.addAll(copyRules); |
| 16 | + if (field.value case List values) { |
| 17 | + final currentSchema = schema as RuleParser; |
| 18 | + final copyRules = currentSchema.rules.toList(); |
18 | 19 |
|
19 | | - currentField.customKeys.add(i.toString()); |
20 | | - schema.parse(ctx, currentField); |
| 20 | + for (int i = 0; i < values.length; i++) { |
| 21 | + final currentField = FieldPool.acquire(field.name, values[i]); |
21 | 22 |
|
22 | | - currentField.customKeys |
23 | | - ..clear() |
24 | | - ..addAll(copy); |
| 23 | + currentSchema.rules.clear(); |
| 24 | + currentSchema.rules.addAll(copyRules); |
25 | 25 |
|
26 | | - currentField.mutate([...field.value, currentField.value]); |
27 | | - FieldPool.release(currentField); |
| 26 | + currentField.customKeys.add(i.toString()); |
| 27 | + schema.parse(ctx, currentField); |
| 28 | + |
| 29 | + currentField.customKeys |
| 30 | + ..clear() |
| 31 | + ..addAll(copy); |
| 32 | + |
| 33 | + currentField.mutate([...field.value, currentField.value]); |
| 34 | + FieldPool.release(currentField); |
| 35 | + } |
| 36 | + |
| 37 | + return; |
28 | 38 | } |
29 | 39 |
|
30 | | - return; |
| 40 | + final error = ctx.errorReporter.format('array', field, null, {}); |
| 41 | + ctx.errorReporter.report('array', [...field.customKeys, field.name], error); |
31 | 42 | } |
| 43 | +} |
| 44 | + |
| 45 | +final class VineArrayUniqueRule implements VineRule { |
| 46 | + final String? message; |
32 | 47 |
|
33 | | - final error = ctx.errorReporter.format('array', field, null, {}); |
34 | | - ctx.errorReporter.report('array', [...field.customKeys, field.name], error); |
| 48 | + const VineArrayUniqueRule(this.message); |
| 49 | + |
| 50 | + @override |
| 51 | + void handle(VineValidationContext ctx, FieldContext field) { |
| 52 | + if (field.value is! List) { |
| 53 | + final error = ctx.errorReporter.format('array.unique', field, message, {}); |
| 54 | + ctx.errorReporter.report('array.unique', [...field.customKeys, field.name], error); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + final values = field.value as List; |
| 59 | + final unique = values.toSet().toList(); |
| 60 | + |
| 61 | + if (values.length != unique.length) { |
| 62 | + final error = ctx.errorReporter.format('array.unique', field, message, {}); |
| 63 | + ctx.errorReporter.report('array.unique', [...field.customKeys, field.name], error); |
| 64 | + } |
| 65 | + } |
35 | 66 | } |
36 | 67 |
|
37 | | -void arrayMinLengthRuleHandler(VineValidationContext ctx, FieldContext field, int minValue, String? message) { |
38 | | - if ((field.value as List).length < minValue) { |
39 | | - final error = ctx.errorReporter.format('array.minLength', field, message, { |
40 | | - 'min': minValue, |
41 | | - }); |
| 68 | +final class VineArrayMinLengthRule implements VineRule { |
| 69 | + final int minValue; |
| 70 | + final String? message; |
| 71 | + |
| 72 | + const VineArrayMinLengthRule(this.minValue, this.message); |
| 73 | + |
| 74 | + @override |
| 75 | + void handle(VineValidationContext ctx, FieldContext field) { |
| 76 | + if ((field.value as List).length < minValue) { |
| 77 | + final error = ctx.errorReporter.format('array.minLength', field, message, { |
| 78 | + 'min': minValue, |
| 79 | + }); |
42 | 80 |
|
43 | | - ctx.errorReporter.report('array.minLength', [...field.customKeys, field.name], error); |
| 81 | + ctx.errorReporter.report('array.minLength', [...field.customKeys, field.name], error); |
| 82 | + } |
44 | 83 | } |
45 | 84 | } |
46 | 85 |
|
47 | | -void arrayMaxLengthRuleHandler(VineValidationContext ctx, FieldContext field, int maxValue, String? message) { |
48 | | - if ((field.value as List).length > maxValue) { |
49 | | - final error = ctx.errorReporter.format('array.maxLength', field, message, { |
50 | | - 'max': maxValue, |
51 | | - }); |
| 86 | +final class VineArrayMaxLengthRule implements VineRule { |
| 87 | + final int maxValue; |
| 88 | + final String? message; |
| 89 | + |
| 90 | + const VineArrayMaxLengthRule(this.maxValue, this.message); |
52 | 91 |
|
53 | | - ctx.errorReporter.report('array.maxLength', [...field.customKeys, field.name], error); |
| 92 | + @override |
| 93 | + void handle(VineValidationContext ctx, FieldContext field) { |
| 94 | + if ((field.value as List).length > maxValue) { |
| 95 | + final error = ctx.errorReporter.format('array.maxLength', field, message, { |
| 96 | + 'max': maxValue, |
| 97 | + }); |
| 98 | + |
| 99 | + ctx.errorReporter.report('array.maxLength', [...field.customKeys, field.name], error); |
| 100 | + } |
54 | 101 | } |
55 | 102 | } |
56 | 103 |
|
57 | | -void arrayFixedLengthRuleHandler(VineValidationContext ctx, FieldContext field, int count, String? message) { |
58 | | - if ((field.value as List).length != count) { |
59 | | - final error = ctx.errorReporter.format('array.fixedLength', field, message, { |
60 | | - 'length': count, |
61 | | - }); |
62 | | - ctx.errorReporter.report('array.fixedLength', [...field.customKeys, field.name], error); |
| 104 | +final class VineArrayFixedLengthRule implements VineRule { |
| 105 | + final int count; |
| 106 | + final String? message; |
| 107 | + |
| 108 | + const VineArrayFixedLengthRule(this.count, this.message); |
| 109 | + |
| 110 | + @override |
| 111 | + void handle(VineValidationContext ctx, FieldContext field) { |
| 112 | + if ((field.value as List).length != count) { |
| 113 | + final error = ctx.errorReporter.format('array.fixedLength', field, message, { |
| 114 | + 'length': count, |
| 115 | + }); |
| 116 | + ctx.errorReporter.report('array.fixedLength', [...field.customKeys, field.name], error); |
| 117 | + } |
63 | 118 | } |
64 | 119 | } |
0 commit comments