From 4faa37188787c223b052def04f3f441561cdb491 Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Thu, 17 Oct 2024 10:00:53 -0500 Subject: [PATCH] Add back required Adding back required so we can distinguish b/w required and nullable --- README.md | 2 +- package.json | 2 +- src/normalizer.ts | 9 +++++---- src/parser.ts | 5 +++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2161483..8b2d20c 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ xtp plugin init \ --schema myschema.yaml \ --template @dylibso/xtp-typescript-bindgen \ --path ./myplugin \ - --feature-flags none + --feature none ``` You can point to a bindgen template on github or directly to a bindgen bundle. diff --git a/package.json b/package.json index 4bb3a9b..8c21e9c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dylibso/xtp-bindgen", - "version": "1.0.0-rc.10", + "version": "1.0.0-rc.11", "description": "XTP bindgen helper library", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/normalizer.ts b/src/normalizer.ts index ee7e034..a12fbcb 100644 --- a/src/normalizer.ts +++ b/src/normalizer.ts @@ -8,6 +8,7 @@ export interface XtpItemType extends Omit { export interface Property extends Omit { '$ref': Schema | null; nullable: boolean; + required: boolean; items?: XtpItemType; name: string; } @@ -158,8 +159,7 @@ function normalizeV1Schema(parsed: parser.V1Schema): XtpSchema { if (s.additionalProperties) { if (s.additionalProperties.$ref) { const refSchema = querySchemaRef(schemas, s.additionalProperties.$ref, `#/components/schemas/${schemaName}/additionalProperties`); - - normalizedSchema.additionalProperties = s as Property; + normalizedSchema.additionalProperties = (s as unknown) as Property; normalizeProp(normalizedSchema.additionalProperties, refSchema, `#/components/schemas/${schemaName}/additionalProperties`); } else { normalizedSchema.additionalProperties = s.additionalProperties as Property; @@ -237,8 +237,9 @@ function normalizeV1Schema(parsed: parser.V1Schema): XtpSchema { validateTypeAndFormat(p.type, p.format, propPath); validateArrayItems(p.items, `${propPath}/items`); - // coerce to false by default + // coerce to false by default for nullable and required p.nullable = p.nullable || false + p.required = !!s.required?.includes(p.name) }) } @@ -396,4 +397,4 @@ function detectCircularReference(schema: Schema, visited: Set = new Set( } return null; -} \ No newline at end of file +} diff --git a/src/parser.ts b/src/parser.ts index bc28749..c0235bd 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -46,6 +46,7 @@ export interface Schema { enum?: string[]; properties?: { [name: string]: Property }; additionalProperties?: Property; + required?: string[]; } export type XtpSchemaType = 'object' | 'enum' | 'map' @@ -88,7 +89,7 @@ export function parseJson(encoded: string): VUnknownSchema { } catch (e) { throw new ValidationError("Invalid JSON", "#"); } - + if (!parsed.version) throw new ValidationError("version property missing", "#"); switch (parsed.version) { case 'v0': @@ -106,4 +107,4 @@ export function isV0Schema(schema: VUnknownSchema): schema is V0Schema { export function isV1Schema(schema: VUnknownSchema): schema is V1Schema { return schema.version === 'v1-draft'; -} \ No newline at end of file +}