Skip to content

Commit

Permalink
feat: builder api (#97)
Browse files Browse the repository at this point in the history
* feat: replace createFormSchema with FormSchemaBuilder

* refactor: class based FormSchemaBuilder

* refactor: reorganize schema builder code

* refactor: reorganize create-form-validator code

* feat: replace createFormValidator with FormValidatorBuilder class

* docs: fix broken and outdated jsdocs for decoders

Co-authored-by: Mikołaj Klaman <mklaman@virtuslab.com>
  • Loading branch information
mixvar and Mikołaj Klaman authored May 12, 2021
1 parent 0ba16ac commit f78bd91
Show file tree
Hide file tree
Showing 34 changed files with 2,782 additions and 2,220 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,26 @@ npm install @virtuslab/formts
#### 2) Define shape of the form

```ts
import { createFormSchema } from "@virtuslab/formts";
import { FormSchemaBuilder, FormFields } from "@virtuslab/formts";

const Schema = createFormSchema(
fields => ({ answer: fields.string() }),
errors => errors<string>()
);
const Schema = new FormSchemaBuilder()
.fields({ answer: FormFields.string() })
.errors<string>()
.build();
```

#### 3) Define validation rules (optional)

```ts
import { createFormValidator } from "@virtuslab/formts";
import { FormValidatorBuilder } from "@virtuslab/formts";

const validator = createFormValidator(Schema, validate => [
validate(
const validator = new FormValidatorBuilder(Schema)
.validate(
Schema.answer,
val => (val === "" ? "Required!" : null),
val => (val !== "42" ? "Wrong answer!" : null)
),
]);
)
.build();
```

#### 3) Create controller holding form state
Expand Down
Loading

0 comments on commit f78bd91

Please sign in to comment.