Skip to content

Commit 7944de3

Browse files
committed
1.0.3 - fix for parsing schemas already containing $path/$pointer/$required
1 parent 5a0fb71 commit 7944de3

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@nlighten/json-schema-utils",
33
"description": "Various utilities for handling JSON Schemas (parse, join, generate, samples)",
4-
"version": "1.0.2",
4+
"version": "1.0.3",
55
"main": "dist/json-schema-utils.js",
66
"umd:main": "dist/json-schema-utils.umd.js",
77
"module": "dist/json-schema-utils.module.js",

src/_tests/parse/parse.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, test, expect } from "vitest";
22
import { parseSchema } from "../../parse/parse";
33
import recursiveSchema from "./schemas/recursive.schema.json";
44
import refSchema from "./schemas/ref.schema.json";
5+
import {ParsedSchemaProperty} from "../../parse/ParsedSchema";
56

67
describe("parseSchema", () => {
78
test("no schema", () => {
@@ -98,4 +99,30 @@ describe("parseSchema", () => {
9899

99100
expect(paths.sample).toBe("hello");
100101
});
102+
103+
test("sample - ignore existing $path", () => {
104+
const paths = parseSchema(
105+
{
106+
$path: "baaahh",
107+
$pointer: "/asdasd/asdasd",
108+
$required: false,
109+
type: "string",
110+
enum: ["hello", "world"],
111+
} as ParsedSchemaProperty,
112+
{ outputSample: true },
113+
);
114+
115+
expect(paths.paths).toEqual([
116+
{
117+
$path: "",
118+
$pointer: "/",
119+
$required: true,
120+
enum: [
121+
"hello",
122+
"world",
123+
],
124+
type: "string",
125+
},
126+
]);
127+
});
101128
});

src/parse/parse.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,16 @@ const _internalParseSchema = (
9898
const simplifiedType = extractSimplifiedType(context, schema);
9999

100100
const prop: ParsedSchemaProperty = {
101-
$path: context.path,
102-
$pointer: context.pointer || "/",
103-
104101
...schema,
105102
type: simplifiedType as any, // might stay empty, don't assume type
103+
104+
$path: context.path,
105+
$pointer: context.pointer || "/",
106106
};
107107
if (is_required) {
108108
prop.$required = is_required;
109+
} else {
110+
delete prop.$required;
109111
}
110112

111113
if (schema.type === "array") {

0 commit comments

Comments
 (0)