Skip to content

Commit

Permalink
1.0.3 - fix for parsing schemas already containing $path/$pointer/$re…
Browse files Browse the repository at this point in the history
…quired
  • Loading branch information
elisherer committed Sep 10, 2024
1 parent 5a0fb71 commit 7944de3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
27 changes: 27 additions & 0 deletions src/_tests/parse/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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",
},
]);
});
});
8 changes: 5 additions & 3 deletions src/parse/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 7944de3

Please sign in to comment.