Current section

Files

Jump to
gleam_validator gen docs validator index.html
Raw

gen/docs/validator/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>validator - validator</title>
<link rel="stylesheet" href="../index.css" type="text/css" />
</head>
<body>
<header class="page-header">
<h2>
validator<!-- - -->
</h2>
</header>
<div class="page">
<nav class="sidebar">
<a class="sidebar-toggle" href="#">
<svg
aria-hidden="true"
focusable="false"
data-prefix="fas"
data-icon="bars"
class="svg-inline--fa fa-bars fa-w-14"
role="img"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 448 512"
>
<path
fill="#fff"
d="M16 132h416c8.837 0 16-7.163 16-16V76c0-8.837-7.163-16-16-16H16C7.163 60 0 67.163 0 76v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16z"
></path>
</svg>
</a>
<h2>Pages</h2>
<ul>
<li><a href="../index.html">README</a></li>
</ul>
<h2>Modules</h2>
<ul>
<li><a href="../validator&#x2f;">validator</a></li>
<li><a href="../validator&#x2f;common&#x2f;">validator&#x2f;common</a></li>
<li><a href="../validator&#x2f;int&#x2f;">validator&#x2f;int</a></li>
<li><a href="../validator&#x2f;list&#x2f;">validator&#x2f;list</a></li>
<li><a href="../validator&#x2f;option&#x2f;">validator&#x2f;option</a></li>
<li><a href="../validator&#x2f;string&#x2f;">validator&#x2f;string</a></li>
</ul>
<h2>Functions</h2>
<ul>
<li><a href="#all">all</a></li>
<li><a href="#and">and</a></li>
<li><a href="#build1">build1</a></li>
<li><a href="#build2">build2</a></li>
<li><a href="#build3">build3</a></li>
<li><a href="#build4">build4</a></li>
<li><a href="#build5">build5</a></li>
<li><a href="#build6">build6</a></li>
<li><a href="#custom">custom</a></li>
<li><a href="#keep">keep</a></li>
<li><a href="#validate">validate</a></li>
<li><a href="#whole">whole</a></li>
</ul>
</nav>
<div class="content">
<h1 class="module-name">validator</h1>
<section class="module-members">
<a href="#module-functions">
<h1 id="module-functions" class="module-member-kind">Functions</h1>
</a>
<div class="member">
<a href="#all">
<h2 id="all" class="member-name">
all
</h2>
</a>
<pre>pub fn all(
validators: List(fn(a) -&gt; Result(a, tuple(b, List(b)))),
) -&gt; fn(a) -&gt; Result(a, tuple(b, List(b)))</pre>
<div class="rendered-markdown"><p>Validate a value using a list of validators.
This runs all the validators in the list.</p>
<p>The initial input is passed to all validators.
All these validators must have the same input and output types.</p>
<p>Returns Ok when all validators pass.
Returns Error when any validator fails. Error will have all failures.</p>
<h2>Example</h2>
<pre><code>let name_validator = v.all([
v_string.is_not_empty(&quot;Empty&quot;),
v_string.min_length(&quot;&gt;=3&quot;, 3),
v_string.max_length(&quot;&lt;=10&quot;, 10)
])
let validator = fn(person: Person) {
v.build1(person)
|&gt; v.validate(person.name, name_validator)
}</code></pre>
</div>
</div>
<div class="member">
<a href="#and">
<h2 id="and" class="member-name">
and
</h2>
</a>
<pre>pub fn and(
validator1: fn(a) -&gt; Result(b, tuple(c, List(c))),
validator2: fn(b) -&gt; Result(d, tuple(c, List(c))),
) -&gt; fn(a) -&gt; Result(d, tuple(c, List(c)))</pre>
<div class="rendered-markdown"><p>Compose validators</p>
<p>Run the first validator and if successful then the second.
Only returns the first error.</p>
<h2>Example</h2>
<pre><code>let name_validator = v_string.is_not_empty(&quot;Empty&quot;)
|&gt; v.and(v_string.min_length(&quot;Must be at least six&quot;, 6))</code></pre>
</div>
</div>
<div class="member">
<a href="#build1">
<h2 id="build1" class="member-name">
build1
</h2>
</a>
<pre>pub fn build1(constructor: a) -&gt; Result(a, b)</pre>
<div class="rendered-markdown"><p>Build a validator for a type that has one attribute</p>
<h2>Example</h2>
<pre><code>type Person { Person(name: String) }
let validator = fn(person: Person) {
v.build1(person)
|&gt; v.validate(person.name, name_validator)
}</code></pre>
</div>
</div>
<div class="member">
<a href="#build2">
<h2 id="build2" class="member-name">
build2
</h2>
</a>
<pre>pub fn build2(
constructor: fn(a, b) -&gt; c,
) -&gt; Result(fn(a) -&gt; fn(b) -&gt; c, d)</pre>
<div class="rendered-markdown"><p>Build a validator for a type that has two attributes</p>
<h2>Example</h2>
<pre><code>type Person { Person(name: String, age: Int) }
let validator = fn(person: Person) {
v.build2(person)
|&gt; v.validate(person.name, name_validator)
|&gt; v.validate(person.age, ...)
}</code></pre>
</div>
</div>
<div class="member">
<a href="#build3">
<h2 id="build3" class="member-name">
build3
</h2>
</a>
<pre>pub fn build3(
constructor: fn(a, b, c) -&gt; d,
) -&gt; Result(fn(a) -&gt; fn(b) -&gt; fn(c) -&gt; d, e)</pre>
<div class="rendered-markdown"><p>Build a validator for a type that has three attributes</p>
<h2>Example</h2>
<pre><code>type Person { Person(name: String, age: Int, email: String) }
let validator = fn(person: Person) {
v.build3(person)
|&gt; v.validate(person.name, name_validator)
|&gt; v.validate(person.age, ...)
|&gt; v.validate(person.email, ...)
}</code></pre>
</div>
</div>
<div class="member">
<a href="#build4">
<h2 id="build4" class="member-name">
build4
</h2>
</a>
<pre>pub fn build4(
constructor: fn(a, b, c, d) -&gt; e,
) -&gt; Result(fn(a) -&gt; fn(b) -&gt; fn(c) -&gt; fn(d) -&gt; e, f)</pre>
<div class="rendered-markdown"><p>Build a validator for a type that has four attributes</p>
</div>
</div>
<div class="member">
<a href="#build5">
<h2 id="build5" class="member-name">
build5
</h2>
</a>
<pre>pub fn build5(
constructor: fn(a, b, c, d, e) -&gt; f,
) -&gt; Result(fn(a) -&gt; fn(b) -&gt; fn(c) -&gt; fn(d) -&gt; fn(e) -&gt; f, g)</pre>
<div class="rendered-markdown"><p>Build a validator for a type that has five attributes</p>
</div>
</div>
<div class="member">
<a href="#build6">
<h2 id="build6" class="member-name">
build6
</h2>
</a>
<pre>pub fn build6(
constructor: fn(a, b, c, d, e, f) -&gt; g,
) -&gt; Result(
fn(a) -&gt; fn(b) -&gt; fn(c) -&gt; fn(d) -&gt; fn(e) -&gt; fn(f) -&gt; g,
h,
)</pre>
<div class="rendered-markdown"><p>Build a validator for a type that has six attributes</p>
</div>
</div>
<div class="member">
<a href="#custom">
<h2 id="custom" class="member-name">
custom
</h2>
</a>
<pre>pub fn custom(
error: a,
check: fn(b) -&gt; Option(c),
) -&gt; fn(b) -&gt; Result(c, tuple(a, List(a)))</pre>
<div class="rendered-markdown"><p>Create a custom validator</p>
<p>A custom validator has two attributes:</p>
<ul>
<li>The error</li>
<li>A check function</li>
</ul>
<p>The check function is a function that takes an <code>input</code> and returns <code>Option(output)</code></p>
<h2>Example</h2>
<pre><code>let must_be_sam = fn(name: String) -&gt; Option(String) {
case name == &quot;Sam&quot; {
True -&gt; Some(name)
False -&gt; None
}
}
let validator = fn(person: Person) {
v.build1(Person)
|&gt; v.validate(person.name, v.custom(&quot;Not Sam&quot;, must_be_sam))
}</code></pre>
</div>
</div>
<div class="member">
<a href="#keep">
<h2 id="keep" class="member-name">
keep
</h2>
</a>
<pre>pub fn keep(
accumulator: Result(fn(a) -&gt; b, tuple(c, List(c))),
value: a,
) -&gt; Result(b, tuple(c, List(c)))</pre>
<div class="rendered-markdown"><p>Keep a value as is.</p>
<h2>Example</h2>
<pre><code>fn person_validor(person: Person) {
v.build2(Person)
|&gt; v.validate(person.name, ...)
|&gt; v.keep(person.age)
}
</code></pre>
</div>
</div>
<div class="member">
<a href="#validate">
<h2 id="validate" class="member-name">
validate
</h2>
</a>
<pre>pub fn validate(
accumulator: Result(fn(a) -&gt; b, tuple(c, List(c))),
value: d,
validator: fn(d) -&gt; Result(a, tuple(c, List(c))),
) -&gt; Result(b, tuple(c, List(c)))</pre>
<div class="rendered-markdown"><p>Validate an attribute.</p>
<h2>Example</h2>
<pre><code>let validator = fn(person: Person) {
v.build1(Person)
|&gt; v.validate(person.name, v_string.is_not_empty(ErrorEmpty))
}
</code></pre>
</div>
</div>
<div class="member">
<a href="#whole">
<h2 id="whole" class="member-name">
whole
</h2>
</a>
<pre>pub fn whole(
validator: fn(a) -&gt; Result(a, b),
) -&gt; fn(Result(a, tuple(b, List(b)))) -&gt;
Result(a, tuple(b, List(b)))</pre>
<div class="rendered-markdown"><p>Validate a structure as a whole.</p>
<p>Sometimes we need to validate a property in relation to another.</p>
<p>This function requires a check function like:</p>
<pre><code>fn(a) -&gt; Result(a, error)
</code></pre>
<h2>Example</h2>
<pre><code>let strengh_and_level_validator = fn(c: Character) {
case c.level &gt; c.strength {
True -&gt; Error(error)
False -&gt; Ok(c)
}
}
let validator = fn(c: Character) {
v.build2(Character)
|&gt; v.validate(c.level, v_int.min(&quot;Level must be more that zero&quot;, 1))
|&gt; v.validate(c.strength, v_int.min(&quot;Strength must be more that zero&quot;, 1))
|&gt; v.whole(strengh_and_level_validator)
}
</code></pre>
</div>
</div>
</section>
</div>
</div>
<footer class="pride" onclick="document.querySelector('.pride').classList.toggle('show')">
<div class="blue">Lucy</div>
<div class="pink">says</div>
<div class="white">trans</div>
<div class="pink">rights</div>
<div class="blue"></div>
<a></a>
</footer>
</body>
</html>