-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathany_schema_example.dart
More file actions
135 lines (114 loc) · 3.92 KB
/
any_schema_example.dart
File metadata and controls
135 lines (114 loc) · 3.92 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
import 'package:ack/ack.dart';
void main() {
print('=== AnySchema Examples ===\n');
// Basic any schema - accepts anything
final anySchema = Ack.any();
print('Basic AnySchema validation:');
print(' 42: ${anySchema.safeParse(42).getOrThrow()}');
print(' "hello": ${anySchema.safeParse("hello").getOrThrow()}');
print(' [1,2,3]: ${anySchema.safeParse([1, 2, 3]).getOrThrow()}');
print(' {"a": 1}: ${anySchema.safeParse({"a": 1}).getOrThrow()}');
print(' true: ${anySchema.safeParse(true).getOrThrow()}');
print(' 3.14: ${anySchema.safeParse(3.14).getOrThrow()}');
// Null handling
print('\nNull handling:');
final nullResult = anySchema.safeParse(null);
print(
' null (non-nullable): ${nullResult.isFail ? "REJECTED" : "ACCEPTED"}',
);
// With nullable support
final nullableAny = Ack.any().nullable();
final nullableResult = nullableAny.safeParse(null);
print(' null (nullable): ${nullableResult.isOk ? "ACCEPTED" : "REJECTED"}');
// With default value
print('\nWith default value:');
final withDefault = Ack.any().withDefault("fallback");
print(
' null input with default: ${withDefault.safeParse(null).getOrThrow()}',
);
print(' 42 input with default: ${withDefault.safeParse(42).getOrThrow()}');
// With refinements for custom validation
print('\nWith refinements:');
final refinedAny = Ack.any().refine(
(value) => value.toString().length > 3,
message: "String representation must be longer than 3 characters",
);
print(
' "hello" (length > 3): ${refinedAny.safeParse("hello").isOk ? "PASS" : "FAIL"}',
);
print(
' "hi" (length <= 3): ${refinedAny.safeParse("hi").isOk ? "PASS" : "FAIL"}',
);
print(
' 12345 (length > 3): ${refinedAny.safeParse(12345).isOk ? "PASS" : "FAIL"}',
);
print(
' 42 (length <= 3): ${refinedAny.safeParse(42).isOk ? "PASS" : "FAIL"}',
);
// JSON Schema generation
print('\nJSON Schema generation:');
final describedSchema = Ack.any()
.describe("Accepts any value")
.withDefault("fallback");
print(' JSON Schema: ${describedSchema.toJsonSchema()}');
// Use cases
print('\n=== Use Cases ===');
// 1. Dynamic configuration
print('\n1. Dynamic configuration:');
final configSchema = Ack.object({
'name': Ack.string(),
'version': Ack.string(),
'settings': Ack.any(), // Accept any configuration structure
});
final config1 = {
'name': 'MyApp',
'version': '1.0.0',
'settings': {'theme': 'dark', 'debug': true},
};
final config2 = {
'name': 'MyApp',
'version': '1.0.0',
'settings': ['feature1', 'feature2', 'feature3'],
};
print(
' Config with object settings: ${configSchema.safeParse(config1).isOk ? "VALID" : "INVALID"}',
);
print(
' Config with array settings: ${configSchema.safeParse(config2).isOk ? "VALID" : "INVALID"}',
);
// 2. API endpoints with flexible payloads
print('\n2. Flexible API payload:');
final apiSchema = Ack.object({
'action': Ack.string(),
'timestamp': Ack.string(),
'payload': Ack.any(), // Accept any payload structure
});
final apiCall = {
'action': 'user_update',
'timestamp': '2024-01-01T00:00:00Z',
'payload': {
'userId': 123,
'changes': {'name': 'John', 'email': 'john@example.com'},
},
};
print(
' API call validation: ${apiSchema.safeParse(apiCall).isOk ? "VALID" : "INVALID"}',
);
// 3. Migration scenarios
print('\n3. Migration scenario:');
final legacySchema =
Ack.object({
'id': Ack.string(),
'data': Ack.any(), // Legacy data can be anything
}).refine(
(obj) => obj['id'] != null && (obj['id'] as String).isNotEmpty,
message: "ID must not be empty",
);
final legacyData = {
'id': 'legacy-123',
'data': 'some old string format', // Could be string, object, array, etc.
};
print(
' Legacy data validation: ${legacySchema.safeParse(legacyData).isOk ? "VALID" : "INVALID"}',
);
}