An ATProto Lexicon validator for Gleam.
at v1.2.0 4.9 kB view raw
1import gleam/json 2import gleeunit 3import gleeunit/should 4import honk/validation/context 5import honk/validation/field 6 7pub fn main() { 8 gleeunit.main() 9} 10 11// Test valid array schema with string items 12pub fn valid_string_array_schema_test() { 13 let schema = 14 json.object([ 15 #("type", json.string("array")), 16 #("items", json.object([#("type", json.string("string"))])), 17 #("minLength", json.int(1)), 18 #("maxLength", json.int(10)), 19 ]) 20 21 let assert Ok(ctx) = context.builder() |> context.build 22 let result = field.validate_array_schema(schema, ctx) 23 result |> should.be_ok 24} 25 26// Test array schema with object items 27pub fn array_with_object_items_test() { 28 let schema = 29 json.object([ 30 #("type", json.string("array")), 31 #( 32 "items", 33 json.object([ 34 #("type", json.string("object")), 35 #( 36 "properties", 37 json.object([ 38 #("name", json.object([#("type", json.string("string"))])), 39 ]), 40 ), 41 ]), 42 ), 43 ]) 44 45 let assert Ok(ctx) = context.builder() |> context.build 46 let result = field.validate_array_schema(schema, ctx) 47 result |> should.be_ok 48} 49 50// Test array schema with nested array items 51pub fn nested_array_schema_test() { 52 let schema = 53 json.object([ 54 #("type", json.string("array")), 55 #( 56 "items", 57 json.object([ 58 #("type", json.string("array")), 59 #("items", json.object([#("type", json.string("integer"))])), 60 ]), 61 ), 62 ]) 63 64 let assert Ok(ctx) = context.builder() |> context.build 65 let result = field.validate_array_schema(schema, ctx) 66 result |> should.be_ok 67} 68 69// Test array schema missing items field 70pub fn missing_items_field_test() { 71 let schema = 72 json.object([ 73 #("type", json.string("array")), 74 #("maxLength", json.int(10)), 75 ]) 76 77 let assert Ok(ctx) = context.builder() |> context.build 78 let result = field.validate_array_schema(schema, ctx) 79 result |> should.be_error 80} 81 82// Test array schema with invalid length constraints 83pub fn invalid_length_constraints_test() { 84 let schema = 85 json.object([ 86 #("type", json.string("array")), 87 #("items", json.object([#("type", json.string("string"))])), 88 #("minLength", json.int(10)), 89 #("maxLength", json.int(5)), 90 ]) 91 92 let assert Ok(ctx) = context.builder() |> context.build 93 let result = field.validate_array_schema(schema, ctx) 94 result |> should.be_error 95} 96 97// Test valid array data 98pub fn valid_array_data_test() { 99 let schema = 100 json.object([ 101 #("type", json.string("array")), 102 #("items", json.object([#("type", json.string("string"))])), 103 #("minLength", json.int(1)), 104 #("maxLength", json.int(5)), 105 ]) 106 107 let data = 108 json.array([json.string("hello"), json.string("world")], fn(x) { x }) 109 110 let assert Ok(ctx) = context.builder() |> context.build 111 let result = field.validate_array_data(data, schema, ctx) 112 result |> should.be_ok 113} 114 115// Test array data below minLength 116pub fn array_below_min_length_test() { 117 let schema = 118 json.object([ 119 #("type", json.string("array")), 120 #("items", json.object([#("type", json.string("string"))])), 121 #("minLength", json.int(3)), 122 ]) 123 124 let data = json.array([json.string("hello")], fn(x) { x }) 125 126 let assert Ok(ctx) = context.builder() |> context.build 127 let result = field.validate_array_data(data, schema, ctx) 128 result |> should.be_error 129} 130 131// Test array data above maxLength 132pub fn array_above_max_length_test() { 133 let schema = 134 json.object([ 135 #("type", json.string("array")), 136 #("items", json.object([#("type", json.string("string"))])), 137 #("maxLength", json.int(2)), 138 ]) 139 140 let data = 141 json.array([json.string("a"), json.string("b"), json.string("c")], fn(x) { 142 x 143 }) 144 145 let assert Ok(ctx) = context.builder() |> context.build 146 let result = field.validate_array_data(data, schema, ctx) 147 result |> should.be_error 148} 149 150// Test array data with invalid item type 151pub fn invalid_item_type_test() { 152 let schema = 153 json.object([ 154 #("type", json.string("array")), 155 #("items", json.object([#("type", json.string("string"))])), 156 ]) 157 158 let data = json.array([json.string("hello"), json.int(42)], fn(x) { x }) 159 160 let assert Ok(ctx) = context.builder() |> context.build 161 let result = field.validate_array_data(data, schema, ctx) 162 result |> should.be_error 163} 164 165// Test empty array with minLength 166pub fn empty_array_with_min_length_test() { 167 let schema = 168 json.object([ 169 #("type", json.string("array")), 170 #("items", json.object([#("type", json.string("string"))])), 171 #("minLength", json.int(1)), 172 ]) 173 174 let data = json.array([], fn(x) { x }) 175 176 let assert Ok(ctx) = context.builder() |> context.build 177 let result = field.validate_array_data(data, schema, ctx) 178 result |> should.be_error 179}