1
0
Fork 0

feat: add optional base schema to schema options

This commit is contained in:
ssube 2021-04-17 18:56:33 -05:00
parent 6f097d8e7e
commit 05d5acfb1e
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
4 changed files with 15 additions and 8 deletions

View File

@ -9,7 +9,7 @@ Safe schema with additional library types added.
<b>Signature:</b>
```typescript
export declare function createSchema(options: Readonly<SchemaOptions>): import("js-yaml").Schema;
export declare function createSchema(options: Readonly<SchemaOptions>): Schema;
```
## Parameters
@ -20,5 +20,5 @@ export declare function createSchema(options: Readonly<SchemaOptions>): import("
<b>Returns:</b>
import("js-yaml").Schema
Schema

View File

@ -8,7 +8,7 @@
| Function | Description |
| --- | --- |
| [createInclude(options)](./js-yaml-schema.createinclude.md) | Instantiate an includer with closure over the provided options. |
| [createInclude(options)](./js-yaml-schema.createinclude.md) | Instantiate an include type with a copy of the provided options, returning the include type and its schema setter. |
| [createSchema(options)](./js-yaml-schema.createschema.md) | Safe schema with additional library types added. |
## Interfaces

View File

@ -14,5 +14,6 @@ export interface SchemaOptions
| Property | Type | Description |
| --- | --- | --- |
| [include](./js-yaml-schema.schemaoptions.include.md) | Readonly&lt;[IncludeOptions](./js-yaml-schema.includeoptions.md)<!-- -->&gt; | |
| [base?](./js-yaml-schema.schemaoptions.base.md) | Schema | <i>(Optional)</i> |
| [include](./js-yaml-schema.schemaoptions.include.md) | Readonly&lt;Omit&lt;[IncludeOptions](./js-yaml-schema.includeoptions.md)<!-- -->, 'schema'&gt;&gt; | |

View File

@ -1,4 +1,5 @@
import { DEFAULT_SCHEMA } from 'js-yaml';
import { mustCoalesce } from '@apextoaster/js-utils';
import { DEFAULT_SCHEMA, Schema } from 'js-yaml';
import { envType } from './type/Env';
import { createInclude, IncludeOptions } from './type/Include';
@ -6,7 +7,8 @@ import { regexpType } from './type/Regexp';
import { streamType } from './type/Stream';
export interface SchemaOptions {
include: Readonly<IncludeOptions>;
base?: Schema;
include: Readonly<Omit<IncludeOptions, 'schema'>>;
}
/**
@ -15,8 +17,12 @@ export interface SchemaOptions {
* @public
*/
export function createSchema(options: Readonly<SchemaOptions>) {
const {includeType, setSchema} = createInclude(options.include);
const schema = DEFAULT_SCHEMA.extend([
const base = mustCoalesce(options.base, DEFAULT_SCHEMA);
const {includeType, setSchema} = createInclude({
...options.include,
schema: base,
});
const schema = base.extend([
envType,
includeType,
regexpType,