Coder Social home page Coder Social logo

Comments (5)

rhys-vdw avatar rhys-vdw commented on May 21, 2024

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)

checkit/core.js

Lines 118 to 119 in 1b4ebea

this.target = _.clone(target || {})
this.context = _.clone(context || {})

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.

skleeschulte avatar skleeschulte commented on May 21, 2024

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.

rhys-vdw avatar rhys-vdw commented on May 21, 2024

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.

skleeschulte avatar skleeschulte commented on May 21, 2024

_.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.

rhys-vdw avatar rhys-vdw commented on May 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.