Comments (5)
Not sure, but you can work around it by passing the context as: { model: bookshelfModelInstance }
since it's a shallow clone.
On Wed, 24 Feb 2016 at 09:50 Stefan Kleeschulte [email protected]
wrote:
These lines (core.js lines 118-119)
Lines 118 to 119 in 1b4ebea
modify the target and context parameters:this.target = _.clone(target || {})
this.context = _.clone(context || {})This breaks for example passing a bookshelf model instance as context.
After the model instance went through _.clone, it doesn't have a get
method anymore. Is there a specific reason for using _.clone here? I
think these statements should be replaced by target || {} and context ||
{} - the user can still decide to use _.clone.—
Reply to this email directly or view it on GitHub
#56.
from checkit.
Yep, that's what I do now. But for me this was unexpected behaviour. I created a pull request to change it if you think there is no reason for the _.clone
. However this might be a breaking change for someone who relies on the implicit _.clone
.
from checkit.
I think that we should keep the original behaviour here. If this were intended to be any data type it would not default to {}
.
from checkit.
_.clone
does not work only with objects:
console.log(typeof _.clone(1));
// number
If the argument has to be an object there should rather be a check like this:
if (typeof xyz !== 'object') throw new TypeError('Argument xyz must be an object.');
But there doesn't seem to be any problem with passing non-object values to context:
checkit.run(body, 1);
This runs without any problems, and I can access the value 1 in a custom validator:
Checkit.Validator.prototype.myValidator = (value, context) => {
// context === 1
};
Even checkit.run(1, 1);
runs without complaining, although this doesn't really make sense. I'll update my pull request, at least think about accepting it. ;)
from checkit.
My comment above was referring to the default value of the context
attribute - an empty object. This suggests that code interfacing with Checkit should assume that it is a POJO.
But for me this was unexpected behaviour.
I'd prefer to update the documentation instead of the behaviour here.
The rationale of this code is that we can't assume that the client will not mutate the context object after passing it as an argument. Personally I don't know if this is necessary, but I don't see any reason to introduce a breaking change here.
from checkit.
Related Issues (20)
- Using context correctly HOT 1
- Contains - can I specify multiple values? HOT 3
- Unexpected validated object
- Why clone the `this.target` object?
- async version (0.7.0) doesn't run HOT 2
- null and '' get a pass? HOT 1
- Nested Validations HOT 7
- Transform the property after validate HOT 2
- Is there a way to send arrays as parameters for custom validator?
- "date" validation problem HOT 1
- `finite` does not seem to exist?
- Better validator documentation
- bcrypt password hash validation
- No validation defined for "max:value" and "min:value"
- Error message for URL
- Unique Name Validation using multiple columns
- email regexp should have a source comment explaining the rationale
- How to get a value of some other field in the same form ?
- Not catching errors at all.
- Front page text truncated HOT 2
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 checkit.