Skip to content

Commit 9c43424

Browse files
feat(property): add default value handling in JsonSchema and related functions (finos#1648)
1 parent 8e357b0 commit 9c43424

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

shared/src/commands/generate/components/property.spec.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
21

3-
import { getConstValue, getEnumPlaceholder, getPropertyValue } from './property';
2+
3+
import { getConstValue, getDefaultValue, getEnumPlaceholder, getPropertyValue } from './property';
44

55
vi.mock('../../../logger', () => {
66
return {
77
initLogger: () => {
88
return {
9-
info: () => {},
10-
debug: () => {}
9+
info: () => { },
10+
debug: () => { }
1111
};
1212
}
1313
};
@@ -39,6 +39,32 @@ describe('getConstValue', () => {
3939
});
4040
});
4141

42+
describe('getDefaultValue', () => {
43+
it('generates default value if default is provided', () => {
44+
expect(getDefaultValue({
45+
'default': 'Example value'
46+
}))
47+
.toBe('Example value');
48+
});
49+
50+
it('generates default value with entire subtree if default is provided', () => {
51+
expect(getDefaultValue({
52+
'default': {
53+
'connects': {
54+
'source': 'source',
55+
'destination': 'destination'
56+
}
57+
}
58+
}))
59+
.toEqual({
60+
'connects': {
61+
'source': 'source',
62+
'destination': 'destination'
63+
}
64+
});
65+
});
66+
});
67+
4268
describe('getPropertyValue', () => {
4369
it('generates string placeholder name from variable', () => {
4470
expect(getPropertyValue('key-name', {
@@ -70,7 +96,7 @@ describe('getPropertyValue', () => {
7096
}))
7197
.toBe('[[ REF_KEY_NAME ]]');
7298
});
73-
99+
74100
it('generates boolean placeholder from variable', () => {
75101
expect(getPropertyValue('key-name', {
76102
'type': 'boolean'

shared/src/commands/generate/components/property.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface JsonSchema {
22
const?: string | object,
3+
default?: string | object,
34
enum?: string[],
45
type?: 'string' | 'integer' | 'number' | 'array' | 'boolean' | 'object',
56
$ref?: string
@@ -27,16 +28,23 @@ export function getBooleanPlaceholder(name: string): string {
2728
* @param detail The detail from the object to instantiate
2829
* @returns Either the value or the object described by the 'const' property.
2930
*/
30-
export function getConstValue(detail: JsonSchema) : string | object {
31+
export function getConstValue(detail: JsonSchema): string | object {
3132
return detail.const;
3233
}
3334

35+
export function getDefaultValue(detail: JsonSchema): string | object {
36+
return detail.default;
37+
}
38+
3439
export function getPropertyValue(keyName: string, detail: JsonSchema): string | string[] | number | object {
35-
// if both const and type are defined, prefer const
40+
// if const, default and type are defined, prefer const
3641
if (detail.const) {
3742
return detail.const;
3843
}
39-
44+
// if default and type are defined, prefer default
45+
if (detail.default) {
46+
return detail.default;
47+
}
4048
if (detail.type) {
4149
const propertyType = detail.type;
4250

0 commit comments

Comments
 (0)