Skip to content

Commit 57033af

Browse files
committed
feat: move around const & annotations
BREAKING CHANGE: meta is removed BREAKING CHANGE: const is no longer a part of annotations BREAKING CHANGE: drop OAS-ish validation keywords
1 parent dd45b48 commit 57033af

File tree

7 files changed

+42
-25
lines changed

7 files changed

+42
-25
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { getAnnotations } from '../getAnnotations';
2+
3+
describe('getAnnotations util', () => {
4+
it('should treat example as examples', () => {
5+
expect(
6+
getAnnotations({
7+
example: 'foo',
8+
}),
9+
).toStrictEqual({
10+
examples: ['foo'],
11+
});
12+
});
13+
14+
it('should prefer examples over example', () => {
15+
expect(
16+
getAnnotations({
17+
examples: ['bar', 'baz'],
18+
example: 'foo',
19+
}),
20+
).toStrictEqual({
21+
examples: ['bar', 'baz'],
22+
});
23+
});
24+
});

src/accessors/getAnnotations.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import type { SchemaAnnotations } from '../nodes/types';
21
import type { SchemaFragment } from '../types';
32
import { pick } from '../utils/pick';
43

5-
const ANNOTATIONS: SchemaAnnotations[] = ['description', 'default', 'examples', 'const', 'x-example'];
4+
const ANNOTATIONS = ['description', 'default', 'examples'] as const;
5+
6+
export type SchemaAnnotations = typeof ANNOTATIONS[number];
67

78
export function getAnnotations(fragment: SchemaFragment) {
8-
return pick(fragment, ANNOTATIONS);
9+
const annotations = pick(fragment, ANNOTATIONS);
10+
if ('example' in fragment && !Array.isArray(annotations.examples)) {
11+
// example is more OAS-ish, but it's common enough to be worth supporting
12+
annotations.examples = [fragment.example];
13+
}
14+
15+
return annotations;
916
}

src/accessors/getMeta.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/nodes/RegularNode.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Dictionary } from '@stoplight/types';
22

33
import { getAnnotations } from '../accessors/getAnnotations';
44
import { getCombiners } from '../accessors/getCombiners';
5-
import { getMeta } from '../accessors/getMeta';
65
import { getPrimaryType } from '../accessors/getPrimaryType';
76
import { getRequired } from '../accessors/getRequired';
87
import { getTypes } from '../accessors/getTypes';
@@ -13,9 +12,10 @@ import { isRegularNode, isSchemaNode } from '../guards';
1312
import type { SchemaFragment } from '../types';
1413
import { BaseNode } from './BaseNode';
1514
import type { ReferenceNode } from './ReferenceNode';
16-
import { MirroredSchemaNode, SchemaAnnotations, SchemaCombinerName, SchemaMeta, SchemaNodeKind } from './types';
15+
import { MirroredSchemaNode, SchemaAnnotations, SchemaCombinerName, SchemaNodeKind } from './types';
1716

1817
export class RegularNode extends BaseNode {
18+
public readonly $id: string | null;
1919
public readonly types: SchemaNodeKind[] | null;
2020
public readonly primaryType: SchemaNodeKind | null; // object (first choice) or array (second option), primitive last
2121
public readonly combiners: SchemaCombinerName[] | null;
@@ -28,24 +28,23 @@ export class RegularNode extends BaseNode {
2828

2929
public children: (RegularNode | ReferenceNode | MirroredSchemaNode)[] | null | undefined;
3030

31-
public readonly meta: Readonly<Partial<Dictionary<unknown, SchemaMeta>>>;
3231
public readonly annotations: Readonly<Partial<Dictionary<unknown, SchemaAnnotations>>>;
3332
public readonly validations: Readonly<Dictionary<unknown>>;
3433

3534
constructor(public readonly fragment: SchemaFragment) {
3635
super(fragment);
3736

37+
this.$id = unwrapStringOrNull('id' in fragment ? fragment.id : fragment.$id);
3838
this.types = getTypes(fragment);
3939
this.primaryType = getPrimaryType(fragment, this.types);
4040
this.combiners = getCombiners(fragment);
4141

4242
this.deprecated = isDeprecated(fragment);
43-
this.enum = unwrapArrayOrNull(fragment.enum);
43+
this.enum = 'const' in fragment ? [fragment.const] : unwrapArrayOrNull(fragment.enum);
4444
this.required = getRequired(fragment.required);
4545
this.format = unwrapStringOrNull(fragment.format);
4646
this.title = unwrapStringOrNull(fragment.title);
4747

48-
this.meta = getMeta(fragment);
4948
this.annotations = getAnnotations(fragment);
5049
this.validations = getValidations(fragment, this.types);
5150

src/nodes/mirrored/MirroredRegularNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { isNonNullable } from '../../utils';
55
import { BaseNode } from '../BaseNode';
66
import type { ReferenceNode } from '../ReferenceNode';
77
import type { RegularNode } from '../RegularNode';
8-
import type { SchemaAnnotations, SchemaCombinerName, SchemaMeta, SchemaNodeKind } from '../types';
8+
import type { SchemaAnnotations, SchemaCombinerName, SchemaNodeKind } from '../types';
99
import { MirroredReferenceNode } from './MirroredReferenceNode';
1010

1111
export class MirroredRegularNode extends BaseNode implements RegularNode {
12+
public readonly $id!: string | null;
1213
public readonly types!: SchemaNodeKind[] | null;
1314
public readonly primaryType!: SchemaNodeKind | null;
1415
public readonly combiners!: SchemaCombinerName[] | null;
@@ -19,7 +20,6 @@ export class MirroredRegularNode extends BaseNode implements RegularNode {
1920
public readonly title!: string | null;
2021
public readonly deprecated!: boolean;
2122

22-
public readonly meta!: Readonly<Partial<Dictionary<unknown, SchemaMeta>>>;
2323
public readonly annotations!: Readonly<Partial<Dictionary<unknown, SchemaAnnotations>>>;
2424
public readonly validations!: Readonly<Dictionary<unknown>>;
2525

src/nodes/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,4 @@ export enum SchemaCombinerName {
2525
OneOf = 'oneOf',
2626
}
2727

28-
export type SchemaAnnotations = 'description' | 'default' | 'examples' | 'const' | 'example' | 'x-example';
29-
30-
export type SchemaMeta = 'id' | '$schema';
28+
export { SchemaAnnotations } from '../accessors/getAnnotations';

src/utils/pick.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Dictionary } from '@stoplight/types';
22

3-
export function pick(target: object, keys: (string | number)[]) {
3+
export function pick(target: object, keys: readonly (string | number)[]) {
44
const source: Dictionary<unknown, string | number> = {};
55

66
for (const key of keys) {

0 commit comments

Comments
 (0)