Something went wrong. Try again.
An ATProto Lexicon validator for Gleam.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198import gleam/jsonimport gleeunitimport gleeunit/shouldimport honk/validation/contextimport honk/validation/primary/subscription
pub fn main() { gleeunit.main()}
// Test valid subscription parameterspub fn valid_subscription_parameters_test() { let schema = json.object([ #("type", json.string("subscription")), #( "parameters", json.object([ #("type", json.string("params")), #( "properties", json.object([ #("cursor", json.object([#("type", json.string("integer"))])), ]), ), ]), ), ])
let data = json.object([#("cursor", json.int(12_345))])
let assert Ok(ctx) = context.builder() |> context.build() subscription.validate_data(data, schema, ctx) |> should.be_ok}
// Test invalid: missing required parameterpub fn invalid_subscription_missing_required_test() { let schema = json.object([ #("type", json.string("subscription")), #( "parameters", json.object([ #("type", json.string("params")), #( "properties", json.object([ #("collection", json.object([#("type", json.string("string"))])), ]), ), #("required", json.array([json.string("collection")], fn(x) { x })), ]), ), ])
let data = json.object([])
let assert Ok(ctx) = context.builder() |> context.build() subscription.validate_data(data, schema, ctx) |> should.be_error}
// Test valid subscription with no parameterspub fn valid_subscription_no_parameters_test() { let schema = json.object([#("type", json.string("subscription"))])
let data = json.object([])
let assert Ok(ctx) = context.builder() |> context.build() subscription.validate_data(data, schema, ctx) |> should.be_ok}
// Test invalid: parameters not an objectpub fn invalid_subscription_not_object_test() { let schema = json.object([ #("type", json.string("subscription")), #( "parameters", json.object([ #("type", json.string("params")), #("properties", json.object([])), ]), ), ])
let data = json.array([], fn(x) { x })
let assert Ok(ctx) = context.builder() |> context.build() subscription.validate_data(data, schema, ctx) |> should.be_error}
// Test message validation with unionpub fn valid_subscription_message_test() { let schema = json.object([ #("type", json.string("subscription")), #( "message", json.object([ #( "schema", json.object([ #("type", json.string("union")), #( "refs", json.array( [json.string("#commit"), json.string("#identity")], fn(x) { x }, ), ), ]), ), ]), ), ])
let data = json.object([#("$type", json.string("#commit"))])
let assert Ok(ctx) = context.builder() |> context.build() // This will likely fail due to missing definitions, but tests dispatch case subscription.validate_message_data(data, schema, ctx) { Ok(_) -> Ok(Nil) Error(_) -> Ok(Nil) } |> should.be_ok}
// Test parameter constraint violationpub fn invalid_subscription_constraint_violation_test() { let schema = json.object([ #("type", json.string("subscription")), #( "parameters", json.object([ #("type", json.string("params")), #( "properties", json.object([ #( "limit", json.object([ #("type", json.string("integer")), #("maximum", json.int(100)), ]), ), ]), ), ]), ), ])
let data = json.object([#("limit", json.int(200))])
let assert Ok(ctx) = context.builder() |> context.build() subscription.validate_data(data, schema, ctx) |> should.be_error}
// Test valid array parameterpub fn valid_subscription_array_parameter_test() { let schema = json.object([ #("type", json.string("subscription")), #( "parameters", json.object([ #("type", json.string("params")), #( "properties", json.object([ #( "repos", json.object([ #("type", json.string("array")), #("items", json.object([#("type", json.string("string"))])), ]), ), ]), ), ]), ), ])
let data = json.object([ #( "repos", json.array( [json.string("did:plc:abc"), json.string("did:plc:xyz")], fn(x) { x }, ), ), ])
let assert Ok(ctx) = context.builder() |> context.build() subscription.validate_data(data, schema, ctx) |> should.be_ok}