Coder Social home page Coder Social logo

Comments (3)

mattphillips avatar mattphillips commented on May 20, 2024 1

Hi @lucianobosco due to a potential vulnerability with unsafely setting the diffed properties on the returned diff object, the diffed object is now created using Object.create(null). This means that the returned diffed object has no prototype and so hasOwnProperty will be undefined.

In your case you could spread the diff object into another object with the default prototype functions available.

      const diffs = diff(originalResource.value, resource.value)
      const d = { ...diffs }
      const payload = {
        ...(d.hasOwnProperty('name') && { name: resource.value.name }),
        ...(d.hasOwnProperty('slug') && { slug: resource.value.slug }),
        ...(d.hasOwnProperty('visible') && { visible: resource.value.visible }),
        ...(d.hasOwnProperty('postCategory') && { post_category_id: resource.value.postCategory?.id || null }),
        ...(d.hasOwnProperty('parent') && { parent_id: resource.value.parent?.id || null })
      }

from deep-object-diff.

lucianobosco avatar lucianobosco commented on May 20, 2024

Hi @lucianobosco due to a potential vulnerability with unsafely setting the diffed properties on the returned diff object, the diffed object is now created using Object.create(null). This means that the returned diffed object has no prototype and so hasOwnProperty will be undefined.

In your case you could spread the diff object into another object with the default prototype functions available.

      const diffs = diff(originalResource.value, resource.value)
      const d = { ...diffs }
      const payload = {
        ...(d.hasOwnProperty('name') && { name: resource.value.name }),
        ...(d.hasOwnProperty('slug') && { slug: resource.value.slug }),
        ...(d.hasOwnProperty('visible') && { visible: resource.value.visible }),
        ...(d.hasOwnProperty('postCategory') && { post_category_id: resource.value.postCategory?.id || null }),
        ...(d.hasOwnProperty('parent') && { parent_id: resource.value.parent?.id || null })
      }

Thanks a lot! That works

from deep-object-diff.

JuanJo4 avatar JuanJo4 commented on May 20, 2024

Note that inner objects doesn't have prototype too, so even if you spread the object you might face the same hasOwnProperty is not a function error.

const diffs = diff(originalResource.value, resource.value)
const d = { ...diffs }

d.innerObj.hasOwnProperty('myProp') // will throw hasOwnProperty is not a function

A different solution than spreading the diff object could be to use Object.hasOwnProperty.call directly, like this:

Object.hasOwnProperty.call(diffs, prop)

from deep-object-diff.

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.