An ATProto Lexicon validator for Gleam.
1import gleam/json
2import gleeunit
3import gleeunit/should
4import honk/validation/context
5import honk/validation/primitive/integer
6
7pub fn main() {
8 gleeunit.main()
9}
10
11// Test valid integer schema
12pub fn valid_integer_schema_test() {
13 let schema =
14 json.object([
15 #("type", json.string("integer")),
16 #("minimum", json.int(0)),
17 #("maximum", json.int(100)),
18 ])
19
20 let assert Ok(ctx) = context.builder() |> context.build
21 let result = integer.validate_schema(schema, ctx)
22 result |> should.be_ok
23}
24
25// Test integer schema with enum
26pub fn integer_with_enum_test() {
27 let schema =
28 json.object([
29 #("type", json.string("integer")),
30 #(
31 "enum",
32 json.array([json.int(1), json.int(2), json.int(3)], fn(x) { x }),
33 ),
34 ])
35
36 let assert Ok(ctx) = context.builder() |> context.build
37 let result = integer.validate_schema(schema, ctx)
38 result |> should.be_ok
39}
40
41// Test invalid integer schema (min > max)
42pub fn invalid_range_constraints_test() {
43 let schema =
44 json.object([
45 #("type", json.string("integer")),
46 #("minimum", json.int(100)),
47 #("maximum", json.int(10)),
48 ])
49
50 let assert Ok(ctx) = context.builder() |> context.build
51 let result = integer.validate_schema(schema, ctx)
52 result |> should.be_error
53}
54
55// Test valid integer data
56pub fn valid_integer_data_test() {
57 let schema =
58 json.object([
59 #("type", json.string("integer")),
60 #("minimum", json.int(0)),
61 #("maximum", json.int(100)),
62 ])
63
64 let data = json.int(42)
65
66 let assert Ok(ctx) = context.builder() |> context.build
67 let result = integer.validate_data(data, schema, ctx)
68 result |> should.be_ok
69}
70
71// Test integer below minimum
72pub fn integer_below_minimum_test() {
73 let schema =
74 json.object([
75 #("type", json.string("integer")),
76 #("minimum", json.int(10)),
77 ])
78
79 let data = json.int(5)
80
81 let assert Ok(ctx) = context.builder() |> context.build
82 let result = integer.validate_data(data, schema, ctx)
83 result |> should.be_error
84}
85
86// Test integer above maximum
87pub fn integer_above_maximum_test() {
88 let schema =
89 json.object([
90 #("type", json.string("integer")),
91 #("maximum", json.int(10)),
92 ])
93
94 let data = json.int(15)
95
96 let assert Ok(ctx) = context.builder() |> context.build
97 let result = integer.validate_data(data, schema, ctx)
98 result |> should.be_error
99}
100
101// Test integer enum validation (valid)
102pub fn integer_enum_valid_test() {
103 let schema =
104 json.object([
105 #("type", json.string("integer")),
106 #(
107 "enum",
108 json.array([json.int(1), json.int(2), json.int(3)], fn(x) { x }),
109 ),
110 ])
111
112 let data = json.int(2)
113
114 let assert Ok(ctx) = context.builder() |> context.build
115 let result = integer.validate_data(data, schema, ctx)
116 result |> should.be_ok
117}
118
119// Test integer enum validation (invalid)
120pub fn integer_enum_invalid_test() {
121 let schema =
122 json.object([
123 #("type", json.string("integer")),
124 #(
125 "enum",
126 json.array([json.int(1), json.int(2), json.int(3)], fn(x) { x }),
127 ),
128 ])
129
130 let data = json.int(5)
131
132 let assert Ok(ctx) = context.builder() |> context.build
133 let result = integer.validate_data(data, schema, ctx)
134 result |> should.be_error
135}
136
137// Test wrong type (string instead of integer)
138pub fn wrong_type_test() {
139 let schema = json.object([#("type", json.string("integer"))])
140
141 let data = json.string("42")
142
143 let assert Ok(ctx) = context.builder() |> context.build
144 let result = integer.validate_data(data, schema, ctx)
145 result |> should.be_error
146}