BSON_VALIDATE(3)

πŸ’… BSON_VALIDATE(3) β€” the libbson one, bestie

okay so like, this is the bson_validate page from libbson, and it's genuinely one of the most useful little functions ever. no cap. let's get into it 🫢


✨ SYNOPSIS ✨

bool
bson_validate (const bson_t *bson, bson_validate_flags_t flags, size_t *offset);

like, that's the whole vibe. three args, one bool back. iconic.


πŸŽ€ PARAMETERS πŸŽ€

  • bson β€” a bson_t. the document you're side-eyeing. πŸ‘€
  • flags β€” a bitwise-or of all the bson_validate_flags_t you want. yes, all of them, or just some, you do you.
  • offset β€” optional spot where the error offset gets written. optional optional. you can literally pass nothing here and it's fine.

πŸ’– DESCRIPTION πŸ’–

so what it does is validate a BSON document β€” it literally walks through the whole thing, inspecting the keys and the values, checking that the content is valid. like a little vibe check for your bytes. 🌸

and you can change up how the validation happens using the flags parameter β€” go look at bson_validate_flags_t for the deets, iykyk.


🌈 RETURNS 🌈

if bson passes the validations you asked for, it returns true. slay. πŸ’…

otherwise it returns false, and β€” if offset is non-NULL β€” that pointer gets written with the byte offset in the document where the error was detected. so you know exactly where it went wrong. very main character of it.

🚨 heads up bestie: if you want more info about the specific validation failure, use bson_validate_with_error_and_offset() instead. this one's just giving you the offset, not the whole story. 😭

πŸ¦‹ SEE ALSO

  • bson_validate_with_error()
  • bson_validate_with_error_and_offset()

also! bson_visitor_t can be used for custom validation β€” see Example Custom Validation. for when you wanna do it your own way, periodt. πŸ”₯


πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc


2009-present, MongoDB, Inc.


and that's the page!! ✨ version 2.5.6, dated Oct 09, 2026 β€” mwah, go validate some BSON, bestie πŸ’œπŸ¦‹

πŸ“¦ where this page comes from