npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

es-mapping-to-schema

v3.3.8

Published

Convert Elasticsearch mappings to Schema Inspector schemas

Downloads

465

Readme

Codacy Badge Coverage Status CircleCI

Elasticsearch Mapping to Schema Inspector schema

This allows you to convert an elasticsearch mapping, defined in JSON into a schema that works with Schema Inspector.

Changelog:

3.2 - Fix bug preventing usage of 'type' or 'properties' as field names. May be a breaking change depending on previous use.

3.1 - Add ability to wildcard paths

3.0 - Completely changed the API and output. Now offers more options, and generates separate schemas for validation and sanitization. Previous version combined validation and sanitization into a single schema, with occasionally bad results.

Install:

npm install --save es-mapping-to-schema

Basic Use:

Pass in the elasticsearch mapping and the options you want. It will return two schemas, one for validation, and one for sanitization.

const MappingToSchema = require('es-mapping-to-schema');

const mapping = {
  properties: {
    booleanThing: {
      type: 'boolean'
    },
    selectors:    {
      properties: {
        selector: {
          properties: {
            name:  {
              type: 'string'
            },
            value: {
              type: 'string'
            }
          }
        }
      }
    }
  }
};

const expectedSchema = {
  type:       'object',
  properties: {
    booleanThing: {
      type: 'boolean'
    },
    selectors:    {
      type:  'array',
      items: {
        selector: {
          type:       'object',
          properties: {
            name:  {
              type: 'string'
            },
            value: {
              type: 'string'
            }
          }
        }
      }
    }
  }
};

// Explicity specify the types to be added to sanitization schema.
const schemas = MappingToSchema(mapping, {
  arrayPaths:   [
    'selectors'
  ],
  sanitization: {
    all: {
      types: [
        'object',
        'string',
        'integer',
        'number',
        'array',
        'boolean',
        'date'
      ]
    }
  }
});

expect(schemas.validation).to.eql(expectedSchema);
expect(schemas.sanitization).to.eql(expectedSchema);

Options:

const Options = {
  // Do not display warnings when an unknown type is encountered
  disableWarnings: false,
  // 'arrayPaths' are used to define properties in the mapping that appear as objects but should be validated as arrays
  // This is because elasticsearch does not explicitly support arrays, but schema inspector does
  arrayPaths:   [],
  // These are the rules and options that will apply only to validation schema generation
  validation:   {
    // 'all' fields are applied recursively to all appropriate fields
    // Currently supports 'strict' and 'optional' for validation
    all:   {
      strict:   false,
      optional: false
    },
    // 'paths' are specific path overrides.
    // For 'paths', any field, value, and path combination is allowed
    // In this case field 'pattern' is applied with value '/must be this/' to property 'path.to.some.property'
    paths: {
      pattern: [
        {
          path: 'path.to.some.property',
          value: /must be this/
        }
      ]
    }
  },
  // These are the rules and options that will apply only to sanitization schema generation
  sanitization: {
    // 'all' fields are applied recursively to all appropriate fields.
    // Currently supports 'strict', 'rules', and 'maxLength' for sanitization.
    // The 'types' field is special, in that you must explicitly list the types you want to sanitize
    // otherwise none will sanitized.
    all:   {
      strict: false,
      rules: [],
      maxLength: 10,
      types: []
    },
    // 'paths' are specific path overrides.
    // For 'paths', any field, value, and path combination is allowed
    // In this case field 'def' is applied with value 'default to this' to property 'path.to.some.property'
    paths: {
      def: [
        {
          path: 'path.to.some.property',
          value: 'default to this'
        },
        {
          path: 'path.to.all.properties.*',
          value: 'default to this'
        }
      ]
    }
  }
};