Skip to content

Getting Started

Chris Michael edited this page Sep 29, 2026 · 4 revisions

Pudu

Getting Started

Install Pudu 0.1.2 or later, then create a project and add the validator:

pudu init my-app
cd my-app
pudu install @chrismichaelps/pudu-lang-validator@0.1.1

A first validator

A validator is a value built from rules. Each rule names a property, selects it with a function, and checks it:

module Ex01First

import Std.Io as Io
import PuduLangValidator as PuduLangValidator
import PuduLangValidator.Rule as Rule
import PuduLangValidator.Rules.Number as NumberRule
import PuduLangValidator.Rules.Text as TextRule
import PuduLangValidator.Validator as Validator

type Customer = { name: Str, age: Int }

fn customerValidator() -> Validator.Validator[Customer] {
  let name = Rule.build(TextRule.notEmpty(Rule.ruleFor("name", |customer: Customer| customer.name)))
  let age = Rule.build(NumberRule.greaterThanOrEqualTo(Rule.ruleFor("age", |customer: Customer| customer.age), 18))
  Validator.add(Validator.add(Validator.create(), name), age)
}

export fn main() -> Int {
  let outcome = Validator.validate(&customerValidator(), Customer{name: "", age: 16})
  let line = "package: " + PuduLangValidator.version()
    + ", valid: " + show(outcome.isValid) + ", failures: " + show(outcome.errors.length())
  match Io.writeLine(line) {
    case Ok(_) => if PuduLangValidator.version() == "0.1.1"
      && !outcome.isValid && outcome.errors.length() == 2 { 0 } else { 2 }
    case Err(_) => 1
  }
}

Check it and run it:

pudu check src/Ex01First.pudu
pudu run src/Ex01First.pudu

Output:

package: 0.1.1, valid: false, failures: 2

Validator.validate returns a result, never an exception. isValid tells whether anything failed; errors holds one entry per failed check, in the order the rules were added. An exported main returning Int sets the process exit status.

Related

Clone this wiki locally