You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: rules first typing: InferInput type and refineRules helper (#112)
* working poc
* temp nor working refine
* handle variants, defineRules, refineRules
* chore: release v1.2.0-beta.1
* chore: test
* reworked some rules
* handle variants in refineRules, added tests
* finished unit tests
* infer state docs
Copy file name to clipboardExpand all lines: docs/src/core-concepts/index.md
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,6 +104,13 @@ Regle provide a list of default rules that you can use from `@regle/rules`.
104
104
105
105
You can find the [list of built-in rules here](/core-concepts/rules/built-in-rules)
106
106
107
+
108
+
:::tip
109
+
110
+
If you prefer to have a rules-first way of typing your state (like schema libraries), you can check [how to do it here](/typescript/infer-state-from-rules)
description: Write your validations in the Zod way
4
+
---
5
+
6
+
# Infer state from rules
7
+
8
+
Regle is state first, that means that you write your rules depending on the state structure, that's the model based way that Vuelidate introduced.
9
+
10
+
With Schema libraries like Zod or Valibot, it's the contrary: the state type depends on the schema output.
11
+
12
+
This mental model may differ to some people, the good new is Regle allow to work both ways!
13
+
14
+
## `InferInput`
15
+
16
+
`InferInput` is an utility type that can produce a object state from any rules object.
17
+
18
+
It will try to extract the possible state type that a rule may have, prioritizing rules that have a strict input type.
19
+
20
+
Some rules may have `unknown` type because it could apply to any value. To cover this, there is now type-helpers rules to help you type your state from the rules: `type`, `string`, `number`, `boolean`, `date`.
21
+
22
+
:::info
23
+
Some types like `numeric` will feel weird as it's typed `string | number`, it's normal as the rule can also validate numeric strings. You can enforce the type by applying `number` rule to it.
Regle is state first because in real world forms, rules can depend a state values.
51
+
This make it a problem for dynamic rules as it would make a cyclic type error when trying to use the state inside the rules.
52
+
53
+
To cover this case and inspired by Zod's `refine`, Regle provides a `refineRules` helper to write dynamic rules that depend on the state, while making it possible to access a typed state.
54
+
55
+
56
+
Anything returned by the rule refine function will override what's defined in the default rules.
0 commit comments