Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

alby

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alby

A JSON validator and safe fallback utility for those rare times you can't trust your config.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

alby

A JSON validator and safe fallback utility for those rare times you can't trust your config.

Love you, boy.

🚀 Getting Started

Using npm:

npm install --save alby

Using yarn:

yarn add alby

⚠️ Warning

It is not recommended at this time suitable to use alby for sanitizing JSON which describes any complex relationships or references between data sources, as these will be malformed.

🤔 How does it work?

jsonschema is a proven tool for defining the expected structure, types and formatting of a particular JSON objects by declaring a corresponding schema. Unfortunately in practice, just defining the schema does not make it so. Poor form validation, developer errors or short-sighted data manipulation all conspire against the frontend developer. This can be particularly common case when third-partys are permitted to bulk datasets to your database. (See: Murphy's Law).

alby builds upon jsonschema by taking its analysis results and in case of error, reverting these back to a safe default value.

In effect, it turns responses like this:

{
  "uuid": "12d31a68-66ba-4857-8263-0512bace0385",
  "branding": "Unknown column '%all%' in 'where clause'",
}

Into something more like this:

{
  "uuid": "12d31a68-66ba-4857-8263-0512bace0385",
  "branding": {
    "backgroundColor": "firebrick",
    "title": "Default Title"
  }
}

Meanwhile, the actual errors from the failed response are still retained. This helps keep your frontend app working in production at a sensible default configuration, whilst you can fire off the failures using an analytics service.

✍️ Example

const { Validator } = require('jsonschema');
const alby = require('alby');

const validator = new Validator();

const schema = {
  id: '/Example',
  type: 'object',
  properties: {
    text: {
      title: 'string',
    },
  },
  required: [
    'title',
  ],
};

const backup = {
  title: 'Default Title',
};

validator.addSchema(
  schema,
);

const getErroneousJson = () => ({
  title: 39248,
});

const {
  result,
  warnings,
} = alby(
  validator,
  schema,
  backup,
  getErroneousJson(),
);
console.log(result); // { title: 'Default Title' },
console.log(warnings); // Lots of warnings!

Please check out the tests for further detail.

🙏 Dependencies

✌️ License

MIT

Keywords

FAQs

Package last updated on 04 Jul 2019

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