Skip to content

Commit 6a0ce13

Browse files
committed
fix: Bug fix for Zod schemas having incorrect title and descriptions
1 parent 01bd2e0 commit 6a0ce13

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codifycli/plugin-core",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "TypeScript library for building Codify plugins to manage system resources (applications, CLI tools, settings) through infrastructure-as-code",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/resource/parsed-resource-settings.test.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,18 @@ describe('Resource options parser tests', () => {
165165
it('Can handle a zod schema', () => {
166166

167167
const schema = z.object({
168-
propA: z.string(),
169-
repository: z.string(),
170-
})
168+
plugins: z
169+
.array(z.string())
170+
.describe(
171+
'Asdf plugins to install. See: https://github.com/asdf-community for a full list'
172+
)
173+
.optional(),
174+
repository: z
175+
.string()
176+
.describe('Remote repository to clone repo from. Equivalent to remote and only one should be specified')
177+
.optional(),
178+
}).meta({ $comment: 'https://codifycli.com/docs/resources/asdf/asdf' })
179+
.describe('This is my test resource');
171180

172181
const option: ResourceSettings<z.infer<typeof schema>> = {
173182
id: 'typeId',
@@ -180,7 +189,29 @@ describe('Resource options parser tests', () => {
180189
}
181190
}
182191

183-
console.log(new ParsedResourceSettings(option))
192+
expect((new ParsedResourceSettings(option)).schema).toMatchObject({
193+
'$schema': 'http://json-schema.org/draft-07/schema#',
194+
type: 'object',
195+
properties: {
196+
plugins: {
197+
description: 'Asdf plugins to install. See: https://github.com/asdf-community for a full list',
198+
type: 'array',
199+
items: {
200+
"type": "string",
201+
},
202+
},
203+
repository: {
204+
description: 'Remote repository to clone repo from. Equivalent to remote and only one should be specified',
205+
type: 'string'
206+
}
207+
},
208+
additionalProperties: false,
209+
title: 'typeId',
210+
description: 'This is my test resource',
211+
'$comment': 'https://codifycli.com/docs/resources/asdf/asdf'
212+
})
213+
214+
console.log((new ParsedResourceSettings(option)).schema)
184215

185216
})
186217
})

src/resource/parsed-resource-settings.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { LinuxDistro, OS, StringIndexedObject } from '@codifycli/schemas';
22
import { JSONSchemaType } from 'ajv';
3-
import { ZodObject, z } from 'zod';
3+
import { z, ZodObject } from 'zod';
44

55
import { StatefulParameterController } from '../stateful-parameter/stateful-parameter-controller.js';
66
import {
77
ArrayParameterSetting,
88
DefaultParameterSetting,
99
InputTransformation,
1010
ParameterSetting,
11-
ResourceSettings,
12-
StatefulParameterSetting,
1311
resolveElementEqualsFn,
1412
resolveEqualsFn,
1513
resolveMatcher,
16-
resolveParameterTransformFn
14+
resolveParameterTransformFn,
15+
ResourceSettings,
16+
StatefulParameterSetting
1717
} from './resource-settings.js';
1818

1919
export interface ParsedStatefulParameterSetting extends DefaultParameterSetting {
@@ -69,8 +69,11 @@ export class ParsedResourceSettings<T extends StringIndexedObject> implements Re
6969
? z.toJSONSchema(schema.strict(), {
7070
target: 'draft-7',
7171
override(ctx) {
72-
ctx.jsonSchema.title = settings.id;
73-
ctx.jsonSchema.description = settings.description ?? `${settings.id} resource. Can be used to manage ${settings.id}`;
72+
if (ctx.path.length === 0) {
73+
ctx.jsonSchema.title = settings.id;
74+
ctx.jsonSchema.description = schema.description ?? settings.description ?? `${settings.id} resource. Can be used to manage ${settings.id}`;
75+
ctx.jsonSchema.$comment = (schema.meta() as Record<string, string | undefined>).$comment;
76+
}
7477
}
7578
}) as JSONSchemaType<T>
7679
: schema;

0 commit comments

Comments
 (0)