diff --git a/docs/dotprompt.md b/docs/dotprompt.md index 1ee2458830..9154e09292 100644 --- a/docs/dotprompt.md +++ b/docs/dotprompt.md @@ -71,6 +71,7 @@ schema: title: string # string, number, and boolean types are defined like this subtitle?: string # optional fields are marked with a `?` draft?: boolean, true when in draft state + status?(enum, approval status): [PENDING, APPROVED] date: string, the date of publication e.g. '2024-04-09' # descriptions follow a comma tags(array, relevant tags for article): string # arrays are denoted via parentheses authors(array): @@ -89,6 +90,8 @@ interface Article { subtitle?: string; /** true when in draft state */ draft?: boolean; + /** approval status */ + status?: 'PENDING' | 'APPROVED'; /** the date of publication e.g. '2024-04-09' */ date: string; /** relevant tags for article */ @@ -107,7 +110,7 @@ interface Article { ``` Picoschema supports scalar types `string`, `integer`, `number`, and `boolean`. For -objects and arrays, they are denoted by a parenthetical after the field name. +objects, arrays, and enums they are denoted by a parenthetical after the field name. Objects defined by Picoschema have all properties as required unless denoted optional by `?`, and do not allow additional properties. diff --git a/js/dotprompt/src/picoschema.ts b/js/dotprompt/src/picoschema.ts index 93abfef97d..e0fcd951cb 100644 --- a/js/dotprompt/src/picoschema.ts +++ b/js/dotprompt/src/picoschema.ts @@ -93,6 +93,8 @@ function parsePico(obj: any, path: string[] = []): JSONSchema { }; } else if (type === 'object') { schema.properties[propertyName] = parsePico(obj[key], [...path, key]); + } else if (type === 'enum') { + schema.properties[propertyName] = { enum: obj[key] }; } else { throw new Error( "Picoschema: parenthetical types must be 'object' or 'array', got: " + diff --git a/js/dotprompt/tests/picoschema_test.ts b/js/dotprompt/tests/picoschema_test.ts index 3028588a5d..b3d6fe8dc6 100644 --- a/js/dotprompt/tests/picoschema_test.ts +++ b/js/dotprompt/tests/picoschema_test.ts @@ -147,6 +147,18 @@ describe('picoschema()', () => { foo: {type: string}`, want: { type: 'object', properties: { foo: { type: 'string' } } }, }, + { + description: 'enum field', + yaml: `schema: + color?(enum, the enum): [RED, BLUE, GREEN]`, + want: { + type: 'object', + properties: { + color: { description: 'the enum', enum: ['RED', 'BLUE', 'GREEN'] }, + }, + additionalProperties: false, + }, + }, ]; for (const test of tests) {