Comments (4)
Hey @celluj34 ,
Can you share an example of your scenario, please?
For custom rules, you can take a look at the documentation section: https://imbrn.dev/v8n/Extending.html
from v8n.
Sure thing, something like the below:
class User {
id: string;
name: string;
canLoginAfter: Date;
canLoginUntil: Date;
}
const user: User = {
id: "<some guid value>",
name: "celluj34",
canLoginAfter: new Date("2022-01-01"),
canLoginUntil: new Date("2021-01-01") //see these dates are in backwards order
}
const result = v8n()
.schema({
canLoginUntil: v8n().greaterThan( /* ??? */ ) // should compare to user.canLoginAfter
})
.test(user) //expected: false
from v8n.
@celluj34 , thank you for sharing your code. :)
tldr; Sorry, but today v8n doesn't have validation based on any kind of context data (like an object being validated by schema rule). But having that feature seems really exciting to me.
We could design something like this:
const myCustomRule = (ruleConfig) = (value, context) => context.prop == ruleConfig.expectedProp && value;
But to achieve that, we'd have to change how validation rules are executed today. But that's really exciting.
I'll make sure to keep this thread linked with a future proposal of this feature so that you can follow it along.
from v8n.
Okay, thanks for the info! Yeah I was hoping to do something similar to FluentValidation from C#, I suppose I could create a new v8n().schema
per object under test, but that seems a bit wasteful if you're creating a lot of them:
const genericUserValidator = (user) => v8n()
.schema({
canLoginUntil: v8n().greaterThan(user.canLoginAfter.valueOf())
});
const userValidator = genericUserValidator(user);
const result = userValidator.test(user) //expected: false
from v8n.
Related Issues (20)
- min() and max() rules in guide HOT 1
- JSON Schema HOT 2
- Version 1.3.0 - v8n().optional() and v8n().string() bug HOT 2
- Bug: nested async rules HOT 1
- Possibility to validate in relation to other rules HOT 4
- Optimise builds depending on consumer capability HOT 5
- Alternating true/false validation on a value HOT 2
- All causes = null when validating v8n().schema using testAll HOT 5
- Schema testing against an object that extends the schema HOT 10
- Publish dist folder to git HOT 2
- Can we have a full example for async rules? HOT 2
- Improve support for CI/CD HOT 2
- Improve schema rule with dependent validations and strict mode
- Using optionality and asynchronous rules in the same chain HOT 6
- Logo has bad contrast on GitHub in dark mode HOT 6
- Doumentation does not deploy with GitHub actions
- Security concern HOT 2
- How to properly extend with typescript? HOT 2
- v8n import typescript - has no call signatures HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v8n.