///|
#custom.serde
#custom.garde
pub(all) struct Endpoint {
#custom.serde(rename="endpointUrl")
#custom.garde(url)
url : String
}///|
test "round-trip an endpoint with Garde" {
let input : Json = { "endpointUrl": "https://EXAMPLE.org:443/docs" }
let wire : Endpoint = @json.from_json(input)
guard wire.validate() is Ok(_) else { fail("invalid endpoint") }
guard wire.to_decoded() is Ok(decoded) else {
fail("could not decode endpoint")
}
guard decoded.to_encoded() is Ok(encoded) else {
fail("could not encode endpoint")
}
assert_eq(encoded.to_json(), { "endpointUrl": "https://example.org/docs" })
}///|
#custom.garde
pub struct Contact {
#custom.garde(length(min="1"))
label : String
#custom.garde(required, length(min="2", max="80"))
name : String?
#custom.garde(range(min="0", max="120"))
age : Int32
#custom.garde(skip)
notes : String
}///|
pub impl @garde.Validate for Contact with fn collect(
garde_self,
garde_report,
garde_path,
) {
{
let garde_value = garde_self.label
let garde_path = garde_path.field("label")
if @garde.length(garde_value, Some(1), None) is Some(message) {
garde_report.add(garde_path, message)
}
}
{
let garde_value = garde_self.name
let garde_path = garde_path.field("name")
if garde_value is None {
garde_report.add(garde_path, "is required")
}
if garde_value is Some(garde_value) {
if @garde.length(garde_value, Some(2), Some(80)) is Some(message) {
garde_report.add(garde_path, message)
}
}
}
let garde_value = garde_self.age
let garde_path = garde_path.field("age")
if @garde.range(garde_value, Some(0), Some(120)) is Some(message) {
garde_report.add(garde_path, message)
}
}///|
#custom.garde
pub struct Credentials {
#custom.garde(ascii, alphanumeric)
username : String
#custom.garde(contains("-"), prefix("ref-"), suffix("-ok"))
reference : String
#custom.garde(custom(check_code))
code : String
}
///|
fn check_code(code : String) -> String? {
if code == "unset" {
Some("choose a code")
} else {
None
}
} if @garde.ascii(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}
if @garde.alphanumeric(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}
}
{
let garde_value = garde_self.reference
let garde_path = garde_path.field("reference")
if @garde.contains(garde_value, "-") is Some(message) {
garde_report.add(garde_path, message)
}
if @garde.prefix(garde_value, "ref-") is Some(message) {
garde_report.add(garde_path, message)
}
if @garde.suffix(garde_value, "-ok") is Some(message) {
garde_report.add(garde_path, message)
}
}
let garde_value = garde_self.code
let garde_path = garde_path.field("code")
if check_code(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}///|
#custom.garde
struct ContactFormats {
#custom.garde(email)
email : String
#custom.garde(url)
website : String
#custom.garde(ip)
address : String
#custom.garde(ipv4)
address_v4 : String
#custom.garde(ipv6)
address_v6 : String
#custom.garde(phone_number)
phone : String
}///|
pub impl @garde.Validate for ContactFormats with fn collect(
garde_self,
garde_report,
garde_path,
) {
{
let garde_value = garde_self.email
let garde_path = garde_path.field("email")
if @garde.email(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}
}
{
let garde_value = garde_self.website
let garde_path = garde_path.field("website")
if @garde.url(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}
}
{
let garde_value = garde_self.address
let garde_path = garde_path.field("address")
if @garde.ip(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}
}
{
let garde_value = garde_self.address_v4
let garde_path = garde_path.field("address_v4")
if @garde.ipv4(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}
}
{
let garde_value = garde_self.address_v6
let garde_path = garde_path.field("address_v6")
if @garde.ipv6(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}
}
let garde_value = garde_self.phone
let garde_path = garde_path.field("phone")
if @garde.phone_number(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}
}| Rule | Accepted input and limits |
|---|---|
| A bare email address parsed by MoonMail, without a display name, angle brackets, comments, or a group. | |
| url | An absolute URL, including schemes such as mailto: and file:. Uses MoonURL in WHATWG mode. |
| ip, ipv4, ipv6 | Bare IPv4 or IPv6 text, without a port, brackets, or zone identifier. IPv4 uses four decimal octets. Uses MoonIP. |
| phone_number | An international number with a country calling code. Uses dialnumber for numbering-plan checks and does not assume a default region. |
///|
#custom.garde
pub struct Reference {
#custom.garde(pattern("^ID-[0-9]+\\.$"))
code : String
} let garde_value = garde_self.code
let garde_path = garde_path.field("code")
if @garde.pattern(garde_value, "^ID-[0-9]+\\.$") is Some(message) {
garde_report.add(garde_path, message)
}///|
#custom.garde
pub struct Confirmation {
#custom.garde(skip)
code : String
#custom.garde(matches(code))
repeated : String
} let garde_value = garde_self.repeated
let garde_path = garde_path.field("repeated")
if garde_value != garde_self.code {
garde_report.add(garde_path, "must match code")
}///|
#custom.garde(allow_unvalidated)
pub struct Directory {
#custom.garde(dive)
owner : Contact
#custom.garde(inner(dive))
contacts : Array[Contact]
description : String
}///|
pub impl @garde.Validate for Directory with fn collect(
garde_self,
garde_report,
garde_path,
) {
{
let garde_value = garde_self.owner
let garde_path = garde_path.field("owner")
@garde.Validate::collect(garde_value, garde_report, garde_path)
}
let garde_value = garde_self.contacts
let garde_path = garde_path.field("contacts")
for garde_index, garde_value in garde_value {
let garde_path = garde_path.index(garde_index)
@garde.Validate::collect(garde_value, garde_report, garde_path)
}
}///|
#custom.garde(field(index="0", ascii))
pub struct Id(String)
///|
#custom.garde
pub enum Update[T] {
NoChange
#custom.garde(field(index="0", dive))
Replace(T)
#custom.garde(field(name="label", length(min="1")))
Rename(label~ : String)
#custom.garde(skip)
Legacy(String)
} Id(garde_inner) => {
let garde_value = garde_inner
let garde_path = garde_path.index(0)
if @garde.ascii(garde_value) is Some(message) {
garde_report.add(garde_path, message)
}
}///|
pub impl[T : @garde.Validate] @garde.Validate for Update[T] with fn collect(
garde_self,
garde_report,
garde_path,
) {
match garde_self {
Replace(garde_inner) => {
let garde_value = garde_inner
let garde_path = garde_path.field("Replace").index(0)
@garde.Validate::collect(garde_value, garde_report, garde_path)
}
Rename(label=garde_field_label) => {
let garde_value = garde_field_label
let garde_path = garde_path.field("Rename").field("label")
if @garde.length(garde_value, Some(1), None) is Some(message) {
garde_report.add(garde_path, message)
}
}
_ => ()
}
}///|
#custom.garde(field(index="0", length(min="2")))
pub struct Sized[T](T)
///|
#custom.garde(bounds(T, [Length]), field(index="0", dive))
pub struct Envelope[T](Sized[T])pub impl[T : @garde.Length] @garde.Validate for Sized[T] with fn collect(pub impl[T : @garde.Length] @garde.Validate for Envelope[T] with fn collect(///|
pub struct Policy {
max_count : Int32
}
///|
#custom.garde(context=Policy)
pub struct Allocation {
#custom.garde(custom_with(check_count))
count : Int32
}
///|
fn check_count(count : Int32, policy : Policy) -> String? {
if count > policy.max_count {
Some("exceeds quota")
} else {
None
}
}
///|
#custom.garde(context=Policy)
pub struct Request {
#custom.garde(inner(dive_with))
allocations : Array[Allocation]
}///|
pub fn Allocation::collect_with(
garde_self : Allocation,
garde_context : Policy,
garde_report : @garde.Report,
garde_path : @garde.Path,
) -> Unit {
let garde_value = garde_self.count
let garde_path = garde_path.field("count")
if check_count(garde_value, garde_context) is Some(message) {
garde_report.add(garde_path, message)
}
}///|
pub fn Allocation::validate_with(
self : Allocation,
context : Policy,
) -> Result[Unit, @garde.Report] {
@garde.validate_using(self, (value, report, path) => {
value.collect_with(context, report, path)
})
} let garde_value = garde_self.allocations
let garde_path = garde_path.field("allocations")
for garde_index, garde_value in garde_value {
let garde_path = garde_path.index(garde_index)
garde_value.collect_with(garde_context, garde_report, garde_path)
}///|
#custom.garde(transparent, field(index="0", length(min="3")))
struct Tag(String)
///|
#custom.garde
struct PublishRequest {
#custom.garde(skip)
published : Bool
#custom.garde(when(is_published, required, dive))
tag : Tag?
}
///|
fn is_published(value : PublishRequest) -> Bool {
value.published
}///|
impl @garde.Validate for Tag with fn collect(
garde_self,
garde_report,
garde_path,
) {
match garde_self {
Tag(garde_inner) => {
let garde_value = garde_inner
let garde_path = garde_path
if @garde.length(garde_value, Some(3), None) is Some(message) {
garde_report.add(garde_path, message)
}
}
}
}
///|
impl @garde.Validate for PublishRequest with fn collect(
garde_self,
garde_report,
garde_path,
) {
let garde_value = garde_self.tag
let garde_path = garde_path.field("tag")
if is_published(garde_self) {
if garde_value is None {
garde_report.add(garde_path, "is required")
}
if garde_value is Some(garde_value) {
@garde.Validate::collect(garde_value, garde_report, garde_path)
}
}
}pub(open) trait Length {
fn garde_length(Self) -> Int
}impl Length for FixedArray[T]fn[T] garde_length(self : FixedArray[T]) -> Intimpl Length for ReadOnlyArray[T]fn[T] garde_length(self : ReadOnlyArray[T]) -> Inttype Pathtype Reportfn ipv6(value : String) -> String?fn pattern(value : String, expression : String) -> String?fn phone_number(value : String) -> String?fn url(value : String) -> String?