@jaseeey/ajv-utils

0.1.3 • Public • Published

AJV Utils

A utility package for ajv which provides additional keywords for usage within your schema.

This was created for a specific purpose and does not necessarily follow any schema specifications, but solely to meet my requirements. If you have any issues or suggestions, contributions are always welcome.

This package was created with guidance from ajv-keywords.

Install

npm install @jaseeey/ajv-utils

or

yarn add @jaseeey/ajv-utils

Usage

const Ajv = require('ajv');
const ajv = new Ajv();

require('@jaseeey/ajv-utils')(ajv);

Keywords

Keywords for objects

renameKeys

This keyword renames properties within an object to the desired name. It's primary use is to validate a payload received from an upstream system and transform the object property names as required in the local system.

The renameKeys property in the schema is represented as an Object where the key is the origin property name and the value is the desired property name.

const schema = {
  type: 'object',
  properties: {
    id: {
      type: 'number'
    },
    name: {
      type: 'string'
    }
  },
  renameKeys: {
    name: 'firstName'
  }
};

const data = {
    id: 123,
    name: 'John'
};

ajv.compile(schema)(data);

// Result object:
// data = {
//     id: 123,
//     firstName: 'John'
// }

License

MIT

Package Sidebar

Install

npm i @jaseeey/ajv-utils

Weekly Downloads

0

Version

0.1.3

License

MIT

Unpacked Size

4.37 kB

Total Files

6

Last publish

Collaborators

  • jaseeey