1.0.1 • Published 6 years ago

json-schema-pick v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

json-schema-pick

Build Status dependencies Status devDependencies Status License: MIT

Pick only subset of properties from json schema. Inspired by lodash _.pick(), that does the same, but for the objects.

This is a very simple script that picks only the subset of the properties from the JSON schema. It also returns the subset of the required fields from JSON schema.

Installation

npm i json-schema-pick

Usage

import jsonSchemaPick from 'json-schema-pick';

const recipeSchema =
{
  $schema: 'http://json-schema.org/draft-06/schema#',
  type: 'object',
  properties: {
    title: { type: 'string', minLength: 10, maxLength: 130 },
    description: { type: 'string', minLength: 300, maxLength: 10000 },
    instructions: { type: 'string', minLength: 50, maxLength: 10000 },
    calorie_count: { type: 'integer', minimum: 0, maximum: 1000 },
    carbohydrate_count: { type: 'integer', minimum: 0, maximum: 100 },
    fiber_count: { type: 'integer', minimum: 0, maximum: 100 },
    protein_count: { type: 'integer', minimum: 0, maximum: 100 },
    fat_count: { type: 'integer', minimum: 0, maximum: 100 },
    preparation_time: { type: 'integer', minimum: 1, maximum: 200 },
  },
  required: ['title', 'description', 'instructions', 'calorie_count', 'carbohydrate_count', 'fiber_count', 'protein_count', 'fat_count', 'preparation_time'],
};

const recipeSchemaWithoutNutritionalValues = jsonSchemaPick(recipeSchema, ['title', 'description', 'instructions', 'preparation_time']);

// recipeSchemaWithoutNutritionalValues object is now:
{
  $schema: 'http://json-schema.org/draft-06/schema#',
  type: 'object',
  properties: {
    title: { type: 'string', minLength: 10, maxLength: 130 },
    description: { type: 'string', minLength: 300, maxLength: 10000 },
    instructions: { type: 'string', minLength: 50, maxLength: 10000 },
    preparation_time: { type: 'integer', minimum: 1, maximum: 200 },
  },
  required: ['title', 'description', 'instructions', 'preparation_time'],
};

License

MIT