Skip to content

Repository files navigation

🔀☑️🎁 Objectro

🔀 Transform and ☑️ validate 🎁 objects.

Why?

  • Destructure/restructure objects based on a map
  • Format specific values (sanitise, normalise, etc.)
  • Validate an object based on rules

Read more in the docs.

How?

constexampleObject={example1: "This is a simple example",example2: "Another simple example",example3: "Another simple example to show arrays",emaxpel4: "Pluck properties from the object and rename them",example5: "DO SOME EXTRA FORMATTING AND RETURN AN OBJECT TO MERGE",example6: "Ignore this property"};objectro.transform(exampleObject,// Pluck specific properties from an object to output"example1",["example2","example3"],// Give a map that transforms one property name to another.{emaxpel4: "example4",// You can also pass a format function to transform that value// further, such as sanitising, normalising or otherwise processing// the value// If the format function returns an object, then that object will// be merged with the output objectexample5: (value,propName)=>({[propName]: value.toLowerCase(),example6: "This is new!"})});// Outputs:// {// example1: "This is a simple example",// example2: "Another simple example",// example3: "Another simple example to show arrays",// example4: "Pluck properties from the object and rename them",// example5: "do some extra formatting and return an object to merge",// example6: "This is new!"// }
objectro.validate({name: "Objectro",version: "1.1.0",lastUpdated: "2022-01-09",keywords: ["transform","validate","object","javascript"],randomNumber: 123,nestedObject: {example: true}},{// Will only return true if all nested rules matchall: {// Will return true if at least one nested rule matchesany: {// Will match positive if input object has either propertyhas: ["name","version","nestedObject.example"]},// Check if property value has one or many possible values// Since this is within the `all` rule, all values must be// present within the property value.has: {keywords: ["transform","validate","javascript"]},// Allows you to use validation rules to validate individual// property values.// Since this is within the `all` rule, all values must be// present within the property value.match: {// Check if the property value type is a stringversion: {type: "string"},// Check if property value is greater than another valuerandomNumber: {gt: 100},// You can also use custom functions to validate propertieslastUpdated: value=>newDate(value).getUTCFullYear()===2019},// Negate the result of the following rulesnot: {has: {// You can also reference nested object properties"nestedObject.example": false}}}});// Outputs:// true

Read more in the docs.

Installation

Browser

<scriptsrc="//unpkg.com/objectro@1.1.0/dist/objectro.umd.js"></script><script>// window.objectro should then be availableconsole.log(objectro);</script>

ES Module

 npm i objectro
yarn add objectro

Then in your source code:

// ES5constobjectro=require("objectro").default;const{ transform, validate }=require("objectro");// ES6importobjectrofrom"objectro";import{transform,validate}from"objectro";// TypeScriptimport{TransformFn}from"objectro/lib/types";// if you ever need access to specific objectro type definitionsimport{transform,validate}from"objectro";

Development

To download external dependencies:

 npm i

To run tests (using Jest):

 npm test

Contribute

Got cool ideas? Have questions or feedback? Found a bug? Post an issue

Added a feature? Fixed a bug? Post a PR

License

Apache 2.0

About

🔀 Transform and ☑️ validate 🎁 objects.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages