Update and structure validator API #41
Replies: 4 comments 3 replies
-
@bitkidd. What you think about moving schema.create({
title: schema.string([
rules.allowNull(),
rules.optional(),
]),
}) If we go with the current API, then schema.create({
title: schema.string.optional.allowNull(),
}) I think, either we should have chain-able API or everything should be moved to rules. |
Beta Was this translation helpful? Give feedback.
-
Someone wondering, why not create a chainable API. For example: title: schema
.string()
.trim()
.escape()
.casefy('titleCase')
.minLength(4)
.unique({ table: 'posts', column: 'title' }) The issue with this approach is composibility. How will you extract these rules to its own variable and then apply them on different schemas? With existing API (an array of rules), we can compose rules as follows. const optionalString = [
rules.optional(),
rules.allowNull()
] And then mix it as follows. title: schema.string([
...optionalString,
rules.escape()
]) |
Beta Was this translation helpful? Give feedback.
-
We already discussed in private with the core team how we could make the Validator API better. And this RFC is in line with our thought. I believe an array syntax like propose can be the best for the DX in terms of visibility. It also allows the developer to easily extract rules, add them dynamically, or even create inline rules. As @targos said in the PR, we should also see how we can help the developer know what rules/modifiers are for what types. I do not know if there is a typing system we can do to solve this. For the moment, I believe having a sub-object with the type under title: schema.string([
rules.common.optional()
rules.common.allowNull()
modifiers.string.trim()
]) |
Beta Was this translation helpful? Give feedback.
-
Alright, so we don't have any push back on not making these changes. I believe, I have to dig a little into the code myself, since some of the changes needs a better integration with TypeScript. For example: When someone uses the rule @bitkidd You okay if I go ahead and start the modification process? |
Beta Was this translation helpful? Give feedback.
-
Discussion thread for Validator API changes.
From
To
Full Rendered Proposal
Original PR
Beta Was this translation helpful? Give feedback.
All reactions