2021-04-17 23:56:33 +00:00
|
|
|
import { mustCoalesce } from '@apextoaster/js-utils';
|
|
|
|
import { DEFAULT_SCHEMA, Schema } from 'js-yaml';
|
2019-11-13 14:01:51 +00:00
|
|
|
|
|
|
|
import { envType } from './type/Env';
|
2020-08-09 01:01:34 +00:00
|
|
|
import { createInclude, IncludeOptions } from './type/Include';
|
2019-11-13 14:01:51 +00:00
|
|
|
import { regexpType } from './type/Regexp';
|
|
|
|
import { streamType } from './type/Stream';
|
|
|
|
|
2021-07-10 20:12:59 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2020-08-09 01:01:34 +00:00
|
|
|
export interface SchemaOptions {
|
2021-04-17 23:56:33 +00:00
|
|
|
base?: Schema;
|
2021-07-10 20:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-07-10 22:38:08 +00:00
|
|
|
export function createSchema(options: SchemaOptions): Schema {
|
2021-07-10 20:12:59 +00:00
|
|
|
const base = mustCoalesce(options.base, DEFAULT_SCHEMA);
|
|
|
|
|
|
|
|
return base.extend([
|
|
|
|
envType,
|
|
|
|
regexpType,
|
|
|
|
streamType,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
export interface IncludeSchemaOptions {
|
|
|
|
base?: Schema;
|
2021-04-17 23:56:33 +00:00
|
|
|
include: Readonly<Omit<IncludeOptions, 'schema'>>;
|
2020-08-09 01:01:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 15:57:05 +00:00
|
|
|
/**
|
2021-07-10 20:12:59 +00:00
|
|
|
* Extended schema with the include type, and auto-configuration
|
|
|
|
* of the include schema.
|
2020-08-01 15:57:05 +00:00
|
|
|
*
|
|
|
|
* @public
|
2021-07-10 22:38:08 +00:00
|
|
|
* @deprecated use `createSchema` unless the include type is needed, since it requires a number of callbacks
|
2020-08-01 15:57:05 +00:00
|
|
|
*/
|
2021-07-10 22:38:08 +00:00
|
|
|
export function createIncludeSchema(options: Readonly<IncludeSchemaOptions>): Schema {
|
2021-04-17 23:56:33 +00:00
|
|
|
const base = mustCoalesce(options.base, DEFAULT_SCHEMA);
|
2021-07-10 22:38:08 +00:00
|
|
|
const { includeType, setSchema } = createInclude({
|
2021-04-17 23:56:33 +00:00
|
|
|
...options.include,
|
|
|
|
schema: base,
|
|
|
|
});
|
|
|
|
const schema = base.extend([
|
2020-08-09 01:01:34 +00:00
|
|
|
envType,
|
|
|
|
includeType,
|
|
|
|
regexpType,
|
|
|
|
streamType,
|
|
|
|
]);
|
|
|
|
|
2021-03-28 00:31:44 +00:00
|
|
|
setSchema(schema);
|
2020-03-29 13:13:57 +00:00
|
|
|
|
2020-08-09 01:01:34 +00:00
|
|
|
return schema;
|
|
|
|
}
|