Skip to content

Commit b51c4b3

Browse files
authored
chore: add support for entity files node types and validation (#2377)
* chore: add support for entity files node types and validation * chore: remove catalogEntity1 format * chore: update redocly/config * tests: update lint tests * chore: remove unnecessary files * tests: update entity-yaml test imports * tests: revert snapshot * tests: update entity-yaml test file * chore: update entity-key-valid test * chore: apply review suggestions * chore: apply review suggestions * chore: add fixme for entity lint methods * chore: apply review suggestions * chore: apply review suggestions * tests: add outdent to entity-key-valid test file * chore: use document instead of whole config in lintEntityFile method * tests: remove unnecessary var * chore: add catalogEntity visitor types * chore: remove unnecessary imports/exports
1 parent d39585e commit b51c4b3

File tree

11 files changed

+752
-12
lines changed

11 files changed

+752
-12
lines changed

.changeset/floppy-rules-like.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@redocly/openapi-core": patch
3+
---
4+
5+
Updated @redocly/config to v0.36.2.

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
],
5454
"dependencies": {
5555
"@redocly/ajv": "^8.11.2",
56-
"@redocly/config": "^0.31.0",
56+
"@redocly/config": "^0.36.2",
5757
"ajv-formats": "^2.1.1",
5858
"colorette": "^1.2.0",
5959
"js-levenshtein": "^1.1.6",
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { createEntityTypes } from '../types/entity-yaml.js';
3+
import { NormalizedNodeType, normalizeTypes, ResolveTypeFn } from '../types/index.js';
4+
import { entityFileSchema, entityFileDefaultSchema } from '@redocly/config';
5+
describe('entity-yaml', () => {
6+
it('should create entity types with discriminator', () => {
7+
const entityTypes = createEntityTypes(entityFileSchema, entityFileDefaultSchema);
8+
9+
expect(entityTypes).toHaveProperty('user');
10+
expect(entityTypes).toHaveProperty('api-operation');
11+
expect(entityTypes).toHaveProperty('data-schema');
12+
expect(entityTypes).toHaveProperty('api-description');
13+
expect(entityTypes).toHaveProperty('service');
14+
expect(entityTypes).toHaveProperty('domain');
15+
expect(entityTypes).toHaveProperty('team');
16+
expect(entityTypes).toHaveProperty('EntityFileDefault');
17+
expect(entityTypes).toHaveProperty('EntityFileArray');
18+
});
19+
20+
it('should resolve entity type to default for array items', () => {
21+
const entityTypes = createEntityTypes(entityFileSchema, entityFileDefaultSchema);
22+
const normalizedTypes = normalizeTypes(entityTypes);
23+
24+
const entityFileArrayNode = normalizedTypes['EntityFileArray'];
25+
26+
const unknownEntity = { key: 'unknown', title: 'Unknown' };
27+
const resolvedType = (entityFileArrayNode.items as ResolveTypeFn)(
28+
unknownEntity,
29+
'root'
30+
) as NormalizedNodeType;
31+
32+
expect(resolvedType).toBeTruthy();
33+
expect(resolvedType.name).toBe('EntityFileDefault');
34+
});
35+
36+
it('should resolve entity type based on discriminator for array items', () => {
37+
const entityTypes = createEntityTypes(entityFileSchema, entityFileDefaultSchema);
38+
const normalizedTypes = normalizeTypes(entityTypes);
39+
40+
const entityFileArrayNode = normalizedTypes['EntityFileArray'];
41+
const userEntity = {
42+
type: 'user',
43+
key: 'john-doe',
44+
title: 'John Doe',
45+
metadata: { email: '[email protected]' },
46+
};
47+
const resolvedType = (entityFileArrayNode.items as ResolveTypeFn)(
48+
userEntity,
49+
'root'
50+
) as NormalizedNodeType;
51+
52+
expect(resolvedType).toBeTruthy();
53+
expect(resolvedType.name).toBe('user');
54+
expect(resolvedType.properties).toHaveProperty('metadata');
55+
});
56+
57+
it('should have correct required fields for entity types', () => {
58+
const entityTypes = createEntityTypes(entityFileSchema, entityFileDefaultSchema);
59+
const normalizedTypes = normalizeTypes(entityTypes);
60+
61+
const userNode = normalizedTypes['user'];
62+
expect(userNode.required).toContain('key');
63+
expect(userNode.required).toContain('title');
64+
expect(userNode.required).toContain('type');
65+
66+
const defaultNode = normalizedTypes['EntityFileDefault'];
67+
expect(defaultNode.required).toContain('type');
68+
expect(defaultNode.required).toContain('key');
69+
expect(defaultNode.required).toContain('title');
70+
});
71+
});

0 commit comments

Comments
 (0)