Skip to content

Add initialValues option to toggle use of type specific initial values #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/draft2019-09/methods/getData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1700,4 +1700,94 @@ describe("getData (2019)", () => {
assert.deepEqual(res, { valid: "stays", invalid: "not removed" });
});
});
describe("defaultTemplateOptions.initialValues", () => {
it("should omit properties without default values when 'initialValues:false' even if required", () => {
const node = compileSchema({
$schema: "draft-2019-09",
type: "object",
required: ["name", "age", "country"],
properties: {
name: { type: "string", default: "John Doe" },
age: { type: "number" },
country: { type: "string", default: "USA" }
}
});
const res = node.getData(undefined, {
extendDefaults: false, initialValues: false
});

assert.deepEqual(res, { name: "John Doe", country: "USA" });
});

it("should omit properties without default values when 'initialValues:false' even if required in nested", () => {
const node = compileSchema({
$schema: "draft-2019-09",
type: "object",
properties: {
user: {
type: "object",
required: ["id", "username"],
properties: {
id: { type: "string" },
username: { type: "string", default: "guest" },
profile: {
type: "object",
properties: {
bio: { type: "string" },
theme: { type: "string", default: "light" }
}
}
}
},
active: { type: "boolean", default: true }
}
});
const res = node.getData({}, { addOptionalProps: true, initialValues: false});

assert.deepEqual(JSON.stringify(res), JSON.stringify({
user: {
username: "guest",
profile: {
theme: "light"
}
},
active: true
}));
});

it("should handle type string with default value and 'initialValues:false'", () => {
const node = compileSchema({
type: "string",
default: "default value"
});
const res = node.getData(undefined, {
initialValues: false
});
assert.deepEqual(res, "default value");
});

it("should handle type string without default value and 'initialValues:false'", () => {
const node = compileSchema({
type: "string",
});
const res = node.getData(undefined, {initialValues: false});
assert.deepEqual(res, undefined);
});

it("should handle array without default value and 'initialValues:false'", () => {
const node = compileSchema({
$schema: "draft-2019-09",
type: "object",
required: ["title"],
properties: {
title: {
type: "array",
items: { type: "string" }
}
}
});
const res = node.getData(undefined, {initialValues: false});
assert.deepEqual(res, { title: [] });
});
});
});
25 changes: 16 additions & 9 deletions src/draft2019-09/methods/getData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export type TemplateOptions = {
* regardless of minItems settings.
*/
extendDefaults?: boolean;
/**
* Set to false to not use type specific initial values.Defaults to true
*/
initialValues?: boolean;
/**
* Limits how often a $ref should be followed before aborting. Prevents infinite data-structure.
* Defaults to 1
Expand Down Expand Up @@ -173,11 +177,11 @@ export function getData(node: SchemaNode, data?: unknown, opts?: TemplateOptions
}

const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptions) => unknown> = {
null: (node, data) => getDefault(node, data, null),
string: (node, data) => getDefault(node, data, ""),
number: (node, data) => getDefault(node, data, 0),
integer: (node, data) => getDefault(node, data, 0),
boolean: (node, data) => getDefault(node, data, false),
null: (node, data, opts) => getDefault(node, data, null, opts.initialValues),
string: (node, data,opts) => getDefault(node, data, "", opts.initialValues),
number: (node, data,opts) => getDefault(node, data, 0, opts.initialValues),
integer: (node, data,opts) => getDefault(node, data, 0, opts.initialValues),
boolean: (node, data,opts) => getDefault(node, data, false, opts.initialValues),
// object: (draft, schema, data: Record<string, unknown> | undefined, pointer: JsonPointer, opts: TemplateOptions) => {
object: (node, data, opts) => {
const schema = node.schema;
Expand All @@ -193,7 +197,10 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
const value = data === undefined || input === undefined ? getValue(template, propertyName) : input;
// Omit adding a property if it is not required or optional props should be added
if (value != null || isRequired || opts.addOptionalProps) {
d[propertyName] = propertyNode.getData(value, opts);
const propertyValue = propertyNode.getData(value, opts);
if(propertyValue !== undefined){
d[propertyName] = propertyValue;
}
}
});
}
Expand Down Expand Up @@ -328,15 +335,15 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
}
};

function getDefault({ schema }: SchemaNode, templateValue: any, initValue: any) {
function getDefault({ schema }: SchemaNode, templateValue: any, initValue: any, initialValues: boolean) {
if (templateValue !== undefined) {
return convertValue(schema.type, templateValue);
} else if (schema.const) {
return schema.const;
} else if (schema.default === undefined && Array.isArray(schema.enum)) {
return schema.enum[0];
} else if (schema.default === undefined) {
} else if (schema.default === undefined && initialValues !== false) {
return initValue;
}
return schema.default;
}
}
87 changes: 87 additions & 0 deletions src/methods/getData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1614,4 +1614,91 @@ describe("getData", () => {
assert.deepEqual(res, { valid: "stays", invalid: "not removed" });
});
});
describe("defaultTemplateOptions.initialValues", () => {
it("should omit properties without default values when 'initialValues:false' even if required", () => {
const node = compileSchema({
type: "object",
required: ["name", "age", "country"],
properties: {
name: { type: "string", default: "John Doe" },
age: { type: "number" },
country: { type: "string", default: "USA" }
}
});
const res = node.getData(undefined, {
extendDefaults: false, initialValues: false
});

assert.deepEqual(res, { name: "John Doe", country: "USA" });
});

it("should omit properties without default values when 'initialValues:false' even if required in nested", () => {
const node = compileSchema({
type: "object",
properties: {
user: {
type: "object",
required: ["id", "username"],
properties: {
id: { type: "string" },
username: { type: "string", default: "guest" },
profile: {
type: "object",
properties: {
bio: { type: "string" },
theme: { type: "string", default: "light" }
}
}
}
},
active: { type: "boolean", default: true }
}
});
const res = node.getData({}, { addOptionalProps: true, initialValues: false});

assert.deepEqual(JSON.stringify(res), JSON.stringify({
user: {
username: "guest",
profile: {
theme: "light"
}
},
active: true
}));
});

it("should handle type string with default value and 'initialValues:false'", () => {
const node = compileSchema({
type: "string",
default: "default value"
});
const res = node.getData(undefined, {
initialValues: false
});
assert.deepEqual(res, "default value");
});

it("should handle type string without default value and 'initialValues:false'", () => {
const node = compileSchema({
type: "string",
});
const res = node.getData(undefined, {initialValues: false});
assert.deepEqual(res, undefined);
});

it("should handle array without default value and 'initialValues:false'", () => {
const node = compileSchema({
type: "object",
required: ["title"],
properties: {
title: {
type: "array",
items: { type: "string" }
}
}
});
const res = node.getData(undefined, {initialValues: false});
assert.deepEqual(res, { title: [] });
});
});
});
25 changes: 16 additions & 9 deletions src/methods/getData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export type TemplateOptions = {
* regardless of minItems settings.
*/
extendDefaults?: boolean;
/**
* Set to false to not use type specific initial values.Defaults to true
*/
initialValues?: boolean;
/**
* Limits how often a $ref should be followed before aborting. Prevents infinite data-structure.
* Defaults to 1
Expand Down Expand Up @@ -164,11 +168,11 @@ export function getData(node: SchemaNode, data?: unknown, opts?: TemplateOptions
}

const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptions) => unknown> = {
null: (node, data) => getDefault(node, data, null),
string: (node, data) => getDefault(node, data, ""),
number: (node, data) => getDefault(node, data, 0),
integer: (node, data) => getDefault(node, data, 0),
boolean: (node, data) => getDefault(node, data, false),
null: (node, data, opts) => getDefault(node, data, null, opts.initialValues),
string: (node, data,opts) => getDefault(node, data, "", opts.initialValues),
number: (node, data,opts) => getDefault(node, data, 0, opts.initialValues),
integer: (node, data,opts) => getDefault(node, data, 0, opts.initialValues),
boolean: (node, data,opts) => getDefault(node, data, false, opts.initialValues),
// object: (draft, schema, data: Record<string, unknown> | undefined, pointer: JsonPointer, opts: TemplateOptions) => {
object: (node, data, opts) => {
const schema = node.schema;
Expand All @@ -184,7 +188,10 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
const value = data === undefined || input === undefined ? getValue(template, propertyName) : input;
// Omit adding a property if it is not required or optional props should be added
if (value != null || isRequired || opts.addOptionalProps) {
d[propertyName] = propertyNode.getData(value, opts);
const propertyValue = propertyNode.getData(value, opts);
if(propertyValue !== undefined){
d[propertyName] = propertyValue;
}
}
});
}
Expand Down Expand Up @@ -324,15 +331,15 @@ const TYPE: Record<string, (node: SchemaNode, data: unknown, opts: TemplateOptio
}
};

function getDefault({ schema }: SchemaNode, templateValue: any, initValue: any) {
function getDefault({ schema }: SchemaNode, templateValue: any, initValue: any, initialValues: boolean) {
if (templateValue !== undefined) {
return convertValue(schema.type, templateValue);
} else if (schema.const) {
return schema.const;
} else if (schema.default === undefined && Array.isArray(schema.enum)) {
return schema.enum[0];
} else if (schema.default === undefined) {
} else if (schema.default === undefined && initialValues !== false) {
return initValue;
}
return schema.default;
}
}