Something went wrong. Try again.
An ATProto Lexicon validator for Gleam.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321import gleam/jsonimport gleeunitimport gleeunit/shouldimport honk/errorsimport honk/validation/contextimport honk/validation/primary/record
pub fn main() { gleeunit.main()}
// Test complete record with nested objects and arrayspub fn complex_record_test() { let schema = json.object([ #("type", json.string("record")), #("key", json.string("tid")), #( "record", json.object([ #("type", json.string("object")), #( "required", json.array([json.string("title"), json.string("tags")], fn(x) { x }), ), #( "properties", json.object([ #( "title", json.object([ #("type", json.string("string")), #("maxLength", json.int(100)), ]), ), #("description", json.object([#("type", json.string("string"))])), #( "tags", json.object([ #("type", json.string("array")), #("items", json.object([#("type", json.string("string"))])), #("maxLength", json.int(10)), ]), ), #( "metadata", json.object([ #("type", json.string("object")), #( "properties", json.object([ #( "views", json.object([#("type", json.string("integer"))]), ), #( "published", json.object([#("type", json.string("boolean"))]), ), ]), ), ]), ), ]), ), ]), ), ])
let assert Ok(ctx) = context.builder() |> context.build let result = record.validate_schema(schema, ctx) result |> should.be_ok}
// Test valid complex record datapub fn complex_record_data_test() { let schema = json.object([ #("type", json.string("record")), #("key", json.string("tid")), #( "record", json.object([ #("type", json.string("object")), #("required", json.array([json.string("title")], fn(x) { x })), #( "properties", json.object([ #("title", json.object([#("type", json.string("string"))])), #( "tags", json.object([ #("type", json.string("array")), #("items", json.object([#("type", json.string("string"))])), ]), ), ]), ), ]), ), ])
let data = json.object([ #("title", json.string("My Post")), #( "tags", json.array([json.string("tech"), json.string("gleam")], fn(x) { x }), ), ])
let assert Ok(ctx) = context.builder() |> context.build let result = record.validate_data(data, schema, ctx) result |> should.be_ok}
// Test record data missing required fieldpub fn complex_record_missing_required_test() { let schema = json.object([ #("type", json.string("record")), #("key", json.string("tid")), #( "record", json.object([ #("type", json.string("object")), #("required", json.array([json.string("title")], fn(x) { x })), #( "properties", json.object([ #("title", json.object([#("type", json.string("string"))])), ]), ), ]), ), ])
let data = json.object([#("description", json.string("No title"))])
let assert Ok(ctx) = context.builder() |> context.build let result = record.validate_data(data, schema, ctx) result |> should.be_error}
// Test deeply nested object structurepub fn deeply_nested_object_test() { let schema = json.object([ #("type", json.string("record")), #("key", json.string("any")), #( "record", json.object([ #("type", json.string("object")), #( "properties", json.object([ #( "level1", json.object([ #("type", json.string("object")), #( "properties", json.object([ #( "level2", json.object([ #("type", json.string("object")), #( "properties", json.object([ #( "level3", json.object([#("type", json.string("string"))]), ), ]), ), ]), ), ]), ), ]), ), ]), ), ]), ), ])
let assert Ok(ctx) = context.builder() |> context.build let result = record.validate_schema(schema, ctx) result |> should.be_ok}
// Test array of arrayspub fn array_of_arrays_test() { let schema = json.object([ #("type", json.string("record")), #("key", json.string("tid")), #( "record", json.object([ #("type", json.string("object")), #( "properties", json.object([ #( "matrix", json.object([ #("type", json.string("array")), #( "items", json.object([ #("type", json.string("array")), #( "items", json.object([#("type", json.string("integer"))]), ), ]), ), ]), ), ]), ), ]), ), ])
let assert Ok(ctx) = context.builder() |> context.build let result = record.validate_schema(schema, ctx) result |> should.be_ok}
// Test missing required field error message at record root levelpub fn record_missing_required_field_message_test() { let schema = json.object([ #("type", json.string("record")), #("key", json.string("tid")), #( "record", json.object([ #("type", json.string("object")), #("required", json.array([json.string("title")], fn(x) { x })), #( "properties", json.object([ #("title", json.object([#("type", json.string("string"))])), ]), ), ]), ), ])
let data = json.object([#("description", json.string("No title"))])
let assert Ok(ctx) = context.builder() |> context.build let assert Error(error) = record.validate_data(data, schema, ctx)
let error_message = errors.to_string(error) error_message |> should.equal("Data validation failed: required field 'title' is missing")}
// Test missing required field error message in nested objectpub fn record_nested_missing_required_field_message_test() { let schema = json.object([ #("type", json.string("record")), #("key", json.string("tid")), #( "record", json.object([ #("type", json.string("object")), #( "properties", json.object([ #("title", json.object([#("type", json.string("string"))])), #( "metadata", json.object([ #("type", json.string("object")), #( "required", json.array([json.string("author")], fn(x) { x }), ), #( "properties", json.object([ #( "author", json.object([#("type", json.string("string"))]), ), #("tags", json.object([#("type", json.string("string"))])), ]), ), ]), ), ]), ), ]), ), ])
let data = json.object([ #("title", json.string("My Post")), #("metadata", json.object([#("tags", json.string("tech"))])), ])
let assert Ok(ctx) = context.builder() |> context.build let assert Error(error) = record.validate_data(data, schema, ctx)
let error_message = errors.to_string(error) error_message |> should.equal( "Data validation failed: metadata: required field 'author' is missing", )}