Comments (8)
@filipstefansson I was finally able to do this little PR #15 😄
Awesome, let me take a look :)
from nexus-validate.
@ysfaran I think your first solution looks clean. Feel free to create a PR with the updates!
from nexus-validate.
@filipstefansson I was finally able to do this little PR #15 😄
from nexus-validate.
@ysfaran I had a look at the PR this morning I think what you implemented is already possible to do.
// grab context from the third argument
validate: ({ number }, _args, context) => ({
id: number().test(
'is-allowed-id',
'given id is not allowed',
// no need to grab context from here
(value: number) => {
return context.user.id === value;
}
),
}),
You can access the context in the third argument on the validate
method.
Does this solve your problem or am I missing something?
from nexus-validate.
@filipstefansson thanks for looking into it, but unfortunately not. What you are suggesting is what I meant with "Alternatives". The problem here is that if you want to reuse is-allowed-id
you would have to copy it over and over again.
Let's say you have 10 mutations/queries and all want to validate the userid (as in your example). You would have to copy it 10 times, which gets quite error prone once you want to adapt something.
A better idea in this case would be to use yup.addMethod
:
yup.addMethod(yup.string, "allowedUserId", function (userId) {
return this.test(function(value){
const allowed = userId === value;;
if (!allowed) {
return this.createError({
message: `${value} is not a valid userId, only ${userId} is allowed`,
});
}
return true;
});
});
Then you could use it anywhere in your code like this (similar to what I said in "Alternatives"):
validate: ({ number }, _args, context) => ({
id: number().allowedUserId(context.user.id)
}),
),
What I want to achieve with this PR is, that you don't need to pass context.user.id
every time again because it's always dependent on the context value. With my PR you could adapt the yup.addMethod
- yup.addMethod(yup.string, "allowedUserId", function (userId) {
+ yup.addMethod(yup.string, "allowedUserId", function () {
return this.test(function(value){
+ const graphQLContext = testContext.options.context
+ const userId = graphQLContext.currencies.includes(value!);
const allowed = userId === value;;
if (!allowed) {
return this.createError({
message: `${value} is not a valid userId, only ${userId} is allowed`,
});
}
return true;
});
});
And then:
validate: ({ number }, _args, context) => ({
- id: number().allowedUserId(context.user.id)
+ id: number().allowedUserId()
),
}),
Things are getting worse if you rely on more than just one context value, e.g database clients and so on, which involve more complex logic.
From user side you have no way to inject the GraphQL context during validation because nexus-validate
triggers the schema validation:
nexus-validate/src/resolver.ts
Line 64 in 314be02
So my suggestion was to inject the context (it has no other use case anyway) as shown in my PR.
I hope this makes things clearer 🙂
from nexus-validate.
It does make it clearer, thank you! I'll get the PR merged asap.
from nexus-validate.
Since this project has been abandoned, I've published an updated version:
https://github.com/peacechen/nexus-validate
That's based off of JoosepAlviste's fork which incorporates the context in the validate callback.
I hope interested parties will join as maintainers in the new version.
from nexus-validate.
🎉 This issue has been resolved in version 1.3.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
from nexus-validate.
Related Issues (7)
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 nexus-validate.