Skip to content

Instantly share code, notes, and snippets.

@gugod
Created January 9, 2025 15:22
Show Gist options
  • Save gugod/a0abafe75a98b262707ee18b26c6afb4 to your computer and use it in GitHub Desktop.
Save gugod/a0abafe75a98b262707ee18b26c6afb4 to your computer and use it in GitHub Desktop.
Data, with built-in validatiors
# ys -l countries.yml
--- !yamlscript/v0/code
defn Countries(& coll):
if every? Country coll:
=>: coll
die: "Invalid Countries"
defn Country(x):
if IsValidCountry(x):
=>: x
die: "Invalid Country $x"
defn IsValidCountry(x):
:: Minimum requirement for a valid Country
and:
IsNonEmptyStr: x.get("name")
IsPositiveNum: x.get("area")
IsPositiveInt: x.get("population")
defn IsNonEmptyStr(x):
and:
string?: x
ne: (empty? "")
defn IsPositiveInt(x):
and:
int?: x
gt: x 0
defn IsPositiveNum(x):
and:
number?: x
gt: x 0
--- !yamlscript/v0/data
countries: !:Countries*
- name: foo
population: 1000
area: 42334.11
climate: Foggy wet.
- name: bar
population: 2000
area: 44242
incomeTax: 55
- name: baz
population: 100
area: 1234.432
planet: mars
- type: Triangle
sides: [3,4,5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment