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';
|
|
|
|
|
2020-08-09 01:01:34 +00:00
|
|
|
export interface SchemaOptions {
|
2021-04-17 23:56:33 +00:00
|
|
|
base?: Schema;
|
|
|
|
include: Readonly<Omit<IncludeOptions, 'schema'>>;
|
2020-08-09 01:01:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 15:57:05 +00:00
|
|
|
/**
|
|
|
|
* Safe schema with additional library types added.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
2021-03-28 00:31:44 +00:00
|
|
|
export function createSchema(options: Readonly<SchemaOptions>) {
|
2021-04-17 23:56:33 +00:00
|
|
|
const base = mustCoalesce(options.base, DEFAULT_SCHEMA);
|
|
|
|
const {includeType, setSchema} = createInclude({
|
|
|
|
...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;
|
|
|
|
}
|