Skip to content

Commit

Permalink
[Rename] kbn-config-schema to osd-config-scheme (#71)
Browse files Browse the repository at this point in the history
* [Rename] kbn-config-schema to osd-config-scheme

Signed-off-by: Kawika Avilla <kavilla414@gmail.com>

* Handle PR comments

Signed-off-by: Kawika Avilla <kavilla414@gmail.com>
  • Loading branch information
kavilla authored and mihirsoni committed Mar 20, 2021
1 parent ce14e23 commit badfcab
Show file tree
Hide file tree
Showing 64 changed files with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# `@kbn/config-schema` — The Kibana config validation library
# `@osd/config-schema` — The OpenSearch Dashboards config validation library

`@kbn/config-schema` is a TypeScript library inspired by Joi and designed to allow run-time validation of the
Kibana configuration entries providing developers with a fully typed model of the validated data.
`@osd/config-schema` is a TypeScript library inspired by Joi and designed to allow run-time validation of the
OpenSearch Dashboards configuration entries providing developers with a fully typed model of the validated data.

## Table of Contents

- [Why `@kbn/config-schema`?](#why-kbnconfig-schema)
- [Why `@osd/config-schema`?](#why-osdconfig-schema)
- [Schema building blocks](#schema-building-blocks)
- [Basic types](#basic-types)
- [`schema.string()`](#schemastring)
Expand Down Expand Up @@ -35,9 +35,9 @@ Kibana configuration entries providing developers with a fully typed model of th
- [Custom validation](#custom-validation)
- [Default values](#default-values)

## Why `@kbn/config-schema`?
## Why `@osd/config-schema`?

Validation of externally supplied data is very important for Kibana. Especially if this data is used to configure how it operates.
Validation of externally supplied data is very important for OpenSearch Dashboards. Especially if this data is used to configure how it operates.

There are a number of reasons why we decided to roll our own solution for the configuration validation:

Expand Down Expand Up @@ -95,7 +95,7 @@ expect(() =>

__Notes:__
* `validate` method throws as soon as the first schema violation is encountered, no further validation is performed.
* when you retrieve configuration within a Kibana plugin `validate` function is called by the Core automatically providing appropriate namespace and context variables (environment name, package info etc.).
* when you retrieve configuration within a OpenSearch Dashboards plugin `validate` function is called by the Core automatically providing appropriate namespace and context variables (environment name, package info etc.).

### Basic types

Expand Down Expand Up @@ -478,8 +478,8 @@ valueSchema.validate({}, { envName: 'dev' });
```

__Notes:__
* The `@kbn/config-schema` neither validates nor coerces the "dereferenced" value and the developer is responsible for making sure that it has the appropriate type.
* The root context that Kibana provides during config validation includes lots of useful properties like `environment name` that can be used to provide a strict schema for production and more relaxed one for development.
* The `@osd/config-schema` neither validates nor coerces the "dereferenced" value and the developer is responsible for making sure that it has the appropriate type.
* The root context that OpenSearch Dashboards provides during config validation includes lots of useful properties like `environment name` that can be used to provide a strict schema for production and more relaxed one for development.

#### `schema.siblingRef()`

Expand All @@ -496,14 +496,14 @@ const valueSchema = schema.object({
```

__Notes:__
* The `@kbn/config-schema` neither validates nor coerces the "dereferenced" value and the developer is responsible for making sure that it has the appropriate type.
* The `@osd/config-schema` neither validates nor coerces the "dereferenced" value and the developer is responsible for making sure that it has the appropriate type.

## Custom validation

Using built-in schema primitives may not be enough in some scenarios or sometimes the attempt to model complex schemas with built-in primitives only may result in unreadable code.
For these cases `@kbn/config-schema` provides a way to specify a custom validation function for almost any schema building block through the `validate` option.
For these cases `@osd/config-schema` provides a way to specify a custom validation function for almost any schema building block through the `validate` option.

For example `@kbn/config-schema` doesn't have a dedicated primitive for the `RegExp` based validation currently, but you can easily do that with a custom `validate` function:
For example `@osd/config-schema` doesn't have a dedicated primitive for the `RegExp` based validation currently, but you can easily do that with a custom `validate` function:

```typescript
const valueSchema = schema.string({
Expand Down Expand Up @@ -552,4 +552,4 @@ const valueSchemaWithFunctionEvaluatedDefault = schema.string({ defaultValue: ()
```

__Notes:__
* `@kbn/config-schema` neither validates nor coerces default value and developer is responsible for making sure that it has the appropriate type.
* `@osd/config-schema` neither validates nor coerces default value and developer is responsible for making sure that it has the appropriate type.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@kbn/config-schema",
"name": "@osd/config-schema",
"main": "./target/out/index.js",
"types": "./target/types/index.d.ts",
"version": "1.0.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "tsc",
"kbn:bootstrap": "yarn build"
"osd:bootstrap": "yarn build"
},
"devDependencies": {
"typescript": "4.0.2",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('isConfigSchema', () => {
expect(isConfigSchema(function () {})).toBe(false);
});

it('returns true as long as `__isKbnConfigSchemaType` is true', () => {
expect(isConfigSchema({ __isKbnConfigSchemaType: true })).toBe(true);
it('returns true as long as `__isOsdConfigSchemaType` is true', () => {
expect(isConfigSchema({ __isOsdConfigSchemaType: true })).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
import { Type } from '../types';

export function isConfigSchema(obj: any): obj is Type<any> {
return obj ? obj.__isKbnConfigSchemaType === true : false;
return obj ? obj.__isOsdConfigSchemaType === true : false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export abstract class Type<V> {
public readonly type: V = null! as V;

// used for the `isConfigSchema` typeguard
public readonly __isKbnConfigSchemaType = true;
public readonly __isOsdConfigSchemaType = true;

/**
* Internal "schema" backed by Joi.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ test('returns value for valid URI as per RFC3986', () => {
'http://tools.ietf.org/html/rfc3986'
);
expect(uriSchema.validate('udp://3domain.local')).toBe('udp://3domain.local');
expect(uriSchema.validate('urn:elastic:kibana')).toBe('urn:elastic:kibana');
expect(uriSchema.validate('urn:opensearch:opensearchDashboards')).toBe('urn:opensearch:opensearchDashboards');
expect(uriSchema.validate('ftp://ftp.ietf.org/rfc/rfc3986.txt')).toBe(
'ftp://ftp.ietf.org/rfc/rfc3986.txt'
);
expect(uriSchema.validate('mailto:Platform.Kibana@elastic.co')).toBe(
'mailto:Platform.Kibana@elastic.co'
expect(uriSchema.validate('mailto:Platform.OpenSearchDashboards@opensearch.co')).toBe(
'mailto:Platform.OpenSearchDashboards@opensearch.co'
);
expect(uriSchema.validate('tel:+500-111-222-333')).toBe('tel:+500-111-222-333');
expect(uriSchema.validate('file:///kibana.log')).toBe('file:///kibana.log');
expect(uriSchema.validate('http://elastic@localhost:9200')).toBe('http://elastic@localhost:9200');
expect(uriSchema.validate('http://elastic:changeme@localhost:9200')).toBe(
'http://elastic:changeme@localhost:9200'
expect(uriSchema.validate('file:///opensearch_dashboards.log')).toBe('file:///opensearch_dashboards.log');
expect(uriSchema.validate('http://opensearch@localhost:9200')).toBe('http://opensearch@localhost:9200');
expect(uriSchema.validate('http://opensearch:changeme@localhost:9200')).toBe(
'http://opensearch:changeme@localhost:9200'
);
expect(uriSchema.validate('ldap://[2001:db8::7]/c=GB?objectClass?one')).toBe(
'ldap://[2001:db8::7]/c=GB?objectClass?one'
Expand Down Expand Up @@ -79,17 +79,17 @@ describe('#scheme', () => {
test('returns value when URI has required scheme', () => {
const uriSchema = schema.uri({ scheme: ['http', 'https'] });

expect(uriSchema.validate('http://elastic.co')).toBe('http://elastic.co');
expect(uriSchema.validate('https://elastic.co')).toBe('https://elastic.co');
expect(uriSchema.validate('http://opensearch.co')).toBe('http://opensearch.co');
expect(uriSchema.validate('https://opensearch.co')).toBe('https://opensearch.co');
});

test('returns error when shorter string', () => {
const uriSchema = schema.uri({ scheme: ['http', 'https'] });

expect(() => uriSchema.validate('ftp://elastic.co')).toThrowErrorMatchingInlineSnapshot(
expect(() => uriSchema.validate('ftp://opensearch.co')).toThrowErrorMatchingInlineSnapshot(
`"expected URI with scheme [http|https]."`
);
expect(() => uriSchema.validate('file:///kibana.log')).toThrowErrorMatchingInlineSnapshot(
expect(() => uriSchema.validate('file:///opensearch_dashboards.log')).toThrowErrorMatchingInlineSnapshot(
`"expected URI with scheme [http|https]."`
);
});
Expand All @@ -104,16 +104,16 @@ describe('#defaultValue', () => {

test('returns value when specified', () => {
expect(
schema.uri({ defaultValue: 'http://localhost:9200' }).validate('http://kibana.local')
).toBe('http://kibana.local');
schema.uri({ defaultValue: 'http://localhost:9200' }).validate('http://opensearch-dashboards.local')
).toBe('http://opensearch-dashboards.local');
});

test('returns value from context when context reference is specified', () => {
expect(
schema.uri({ defaultValue: schema.contextRef('some_uri') }).validate(undefined, {
some_uri: 'http://kibana.local',
some_uri: 'http://opensearch-dashboards.local',
})
).toBe('http://kibana.local');
).toBe('http://opensearch-dashboards.local');
});
});

Expand All @@ -125,15 +125,15 @@ describe('#validate', () => {
calledWith = val;
};

schema.uri({ validate: validator }).validate('http://kibana.local');
schema.uri({ validate: validator }).validate('http://opensearch-dashboards.local');

expect(calledWith).toBe('http://kibana.local');
expect(calledWith).toBe('http://opensearch-dashboards.local');
});

test('is not called with default value in no input', () => {
const validate = jest.fn();

schema.uri({ validate, defaultValue: 'http://kibana.local' }).validate(undefined);
schema.uri({ validate, defaultValue: 'http://opensearch-dashboards.local' }).validate(undefined);

expect(validate).not.toHaveBeenCalled();
});
Expand All @@ -142,7 +142,7 @@ describe('#validate', () => {
const validate = () => 'validator failure';

expect(() =>
schema.uri({ validate }).validate('http://kibana.local')
schema.uri({ validate }).validate('http://opensearch-dashboards.local')
).toThrowErrorMatchingInlineSnapshot(`"validator failure"`);
});
});
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit badfcab

Please sign in to comment.