Skip to content

Instantly share code, notes, and snippets.

@ingydotnet
Forked from gugod/countries.yml
Last active January 9, 2025 23:04
Show Gist options
  • Save ingydotnet/760677a91720e1152228aaa2b6f8df73 to your computer and use it in GitHub Desktop.
Save ingydotnet/760677a91720e1152228aaa2b6f8df73 to your computer and use it in GitHub Desktop.
Data, with built-in validatiors
!yamlscript/v0/data
:: use(schema-country)
# Annotate countries list:
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]
# Annotate each country:
more countries:
- !:country
name: foo
population: 1000
area: 42334.11
climate: Foggy wet.
- !:country
name: bar
population: 2000
area: 44242
incomeTax: 55
- !:country
name: baz
population: 100
area: 1234.432
planet: mars
# - !:country
# type: Triangle
# sides: [3,4,5]
!yamlscript/v0
defn non-empty-string?(x):
x:string? && x:empty?:not
defn positive-int?(x):
x:int? && gt(0)
defn positive-num?(x):
x:number? && gt(0)
!yamlscript/v0
# use: github:yaml/schemays # Use from http later
use: schemays
use: schema-common
defn countries(xs):
schemays/validate xs:
\(every? country xs)
"Invalid countries"
defn country(x):
:: Minimum requirement for a valid Country
schemays/validate x:
-[\(_.name:non-empty-string?)
\(_.area:positive-num?)
\(_.population:positive-int?)]
"Invalid country"
!yamlscript/v0
ns: schemays
defn validate(val checks err='Validation error'):
checks =: checks:seqable?.if(checks [checks])
mapv _ checks:
fn(check):
when-not check(val):
die: err
=>: val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment