solnic / drops

🛠️ Tools for working with data effectively - data contracts using types, schemas, domain validation rules, type-safe casting, and more.
Other
258 stars 7 forks source link

Type shortcuts #24

Closed solnic closed 1 year ago

solnic commented 1 year ago

This adds convenient type-check shortcuts:

%{
  # translates to required(:test) => type(:string)
  required(:test) => string(),

  # translates to required(:test) => type(:integer)
  required(:test) => integer(),

  # translates to required(:test) => type(:map)
  required(:test) => map()
}

You can use regular predicate syntax with these shortcuts too:

%{
  # translates to required(:test) => type(:string, [:filled?])
  required(:test) => string(:filled?),

  # translates to required(:test) => type(:integer, [:even?, gt?: 2])
  required(:test) => integer([:even?, gt?: 2]),

  # translates to required(:test) => type(:map, size?: 3)
  required(:test) => map(size?: 3)
}