0.9.6 • Published 5 years ago

es-schemas v0.9.6

Weekly downloads
4
License
MIT
Repository
-
Last release
5 years ago

es-schemas

Small lib for generating elasticsearch request body schemas.

  • Search/Query
  • Mappings (TODO)
  • Index Settings (TODO)
  • Repositories (TODO)

Basic usage

import { buildSearchSchema } from 'es-schemas'

const elasticsearchVersion = '7.3.0';
const indexMetaData = psuedoCodeToFetchIndexConfiguration(["my_index"]); // GET URL/my_index

// Generate a search schema which takes index fields into account, which can be used
// to validate queries or assist with typeahead in editors which support json schemas.
const schema = buildSearchSchema(elasticsearchVersion, indexMetaData);

Adding Schema Definitions

Each type of query should export it's own property for defintion. Here is a basic example for a hyphothetical query called foo.

import { SchemaSupplier, ElasticsearchDetail, SchemaProperty } from '../../../../index';

const supplier: SchemaSupplier = (detail: ElasticsearchDetail): SchemaProperty => {
    const foo = new SchemaProperty("foo");

    // Describe the query foo
    foo.description = "The foo query matches all documents which contain bar";

    // Default value (for typeahead, etc)
    foo.default = {
      foo: "bar"
    };

    // Add a property of the query foo
    foo.addProp("foo", "string", "The foo", "bar");

    // Add an enum type field
    foo.addEnumProp("is_bar", ["yes", "no"], "Is this thing a bar?");

    return foo;
}

export default supplier;

Testing a Definition

Ideally each sub query has a corresponding test to ensure it's emitting the correct things.

// File: index.test.ts (although, it can be any file with test.ts)
// Import the property generator to test at top of file
import { default as my_query } from '../src/schemas/path/to/file';

// Testing a given sub-query
test('my query output', () => {
  // Generate the JSON object fragment for the query with ES version 6
  let output = my_query(es6Detail()).toJsonObject();

  // Check that the outout defines given props / defs, etc
  expect(output["type"]).toEqual("object");
  expect(output["properties"]["my_prop"]).toBeDefined();
  expect(output["defintions"]["my_def"]).toBeDefined();
});

Use with Monaco

monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
  validate: true,
  allowComments: false,
  enableSchemaRequest: false,
  schemas: [
    buildSearchSchema(elasticsearchVersion, indexMetaData)
  ]
});
0.9.6

5 years ago

0.9.5

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago