New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

commodo-fields-object

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

commodo-fields-object

A simple object field, used with the Commodo "withFields" higher order function.

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
477
increased by1.92%
Maintainers
1
Weekly downloads
 
Created
Source

commodo-fields-object

Build Status Covergrowth Status code style: prettier All Contributors PRs Welcome

A simple object field, used with the Commodo withFields higher order function.

Install

npm install --save commodo-fields-object

Or if you prefer yarn:

yarn add commodo-fields-object

Quick Example:

import { compose } from "ramda";
import { withFields, string } from "@commodo/fields";
import { object } from "commodo-fields-object";

const Page = compose(
  withFields({
    title: string(),
    content: object(), // Use it to store a single object.
    previousContent: object({ list: true }) // Or use it to store a list of objects.
    // Other fields you might need...
  }),
  // Other higher order functions (HOFs) you might need...
)();

const page = new Page();
company.name = "Welcome!";

// The object field can only accepts objects (with any structure). 
company.content = {
  elements: [
    {
      type: "xyz",
      text: "Lorem ipsum ...",
      elements: [{ type: "image", src: "https://..." }]
    }
  ]
};

company.previousContent = [
  { elements: [ ... ] },
  { elements: [ ... ] },
  { elements: [ ... ] }
];

// The following will throw the WithFieldsError error.
company.content = "A string...";
company.previousContent = [
  { elements: [ ... ] },
  "A string...",
  { elements: [ ... ] }
];

Note: alternatively, you could've also used the populate method to assign the data:

const company = new Company();
company.populate = {
  name: "Welcome!",
  content: {
    elements: [
      {
        type: "xyz",
        text: "Lorem ipsum ...",
        elements: [{ type: "image", src: "https://..." }]
      }
    ]
  },
  previousContent: [
    { elements: [ ... ] },
    { elements: [ ... ] },
    { elements: [ ... ] }
  ]
};

When to use this field?

This field can be used when you don't have a fixed structure of the data that's going to be assigned to it. The above example shows a perfect situation where this is necessary. Note that, although the field does allow any size of the object to be assigned, it's up to you to establish proper data validation and keep your code safe.

And finally, have in mind that, if it turns out that the data that's going to be assigned to the field, actually has a fixed structure, it's always best to use the fields field.

Contributors

Thanks goes to these wonderful people (emoji key):


Adrian Smijulj

💻 📖 💡 👀 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

Keywords

FAQs

Package last updated on 18 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc