diff --git a/package.json b/package.json index ecc8cac..3107144 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@nlighten/json-schema-utils", "description": "Various utilities for handling JSON Schemas (parse, join, generate, samples)", - "version": "1.0.2", + "version": "1.0.3", "main": "dist/json-schema-utils.js", "umd:main": "dist/json-schema-utils.umd.js", "module": "dist/json-schema-utils.module.js", diff --git a/src/_tests/parse/parse.test.ts b/src/_tests/parse/parse.test.ts index f6d00bf..bd2047b 100644 --- a/src/_tests/parse/parse.test.ts +++ b/src/_tests/parse/parse.test.ts @@ -2,6 +2,7 @@ import { describe, test, expect } from "vitest"; import { parseSchema } from "../../parse/parse"; import recursiveSchema from "./schemas/recursive.schema.json"; import refSchema from "./schemas/ref.schema.json"; +import {ParsedSchemaProperty} from "../../parse/ParsedSchema"; describe("parseSchema", () => { test("no schema", () => { @@ -98,4 +99,30 @@ describe("parseSchema", () => { expect(paths.sample).toBe("hello"); }); + + test("sample - ignore existing $path", () => { + const paths = parseSchema( + { + $path: "baaahh", + $pointer: "/asdasd/asdasd", + $required: false, + type: "string", + enum: ["hello", "world"], + } as ParsedSchemaProperty, + { outputSample: true }, + ); + + expect(paths.paths).toEqual([ + { + $path: "", + $pointer: "/", + $required: true, + enum: [ + "hello", + "world", + ], + type: "string", + }, + ]); + }); }); diff --git a/src/parse/parse.ts b/src/parse/parse.ts index 83a315d..ce8f01d 100644 --- a/src/parse/parse.ts +++ b/src/parse/parse.ts @@ -98,14 +98,16 @@ const _internalParseSchema = ( const simplifiedType = extractSimplifiedType(context, schema); const prop: ParsedSchemaProperty = { - $path: context.path, - $pointer: context.pointer || "/", - ...schema, type: simplifiedType as any, // might stay empty, don't assume type + + $path: context.path, + $pointer: context.pointer || "/", }; if (is_required) { prop.$required = is_required; + } else { + delete prop.$required; } if (schema.type === "array") {