๐ฎCreating Your First Rule
In this section we will see how to create your first rule with Polyrule. You can choose one of supported data format for writing it, for our examples, we are going to use json
.
Let's create a rule for manage user creation :
In our example, we need to validate two fields : username
and password
. Like you can see, it's pretty easy read it. We have, for each object, three main part :
type
, that allows you to define a strict type.message
, that allows you to define custom message for your rule, you can give it any type, like a string forusername
or an object forpassword
.rules
, that allows you to declare your rules, in our example, we useregex
,min
andmax
.
Now, we want to translate it to javascript
for example, let's see how to use Polyrule. Open a terminal then run the following command in it :
New file called user.js
should be created in your current directory :
Data format can be a little different depending on the programming language, if you use go
, you can't return mixed
value, we need to create two methods instead of withErrors
parameter.
If you want to use this example, you can import it in your wanted javascript file and use it like that :
Output should be :
Last updated