Skip to content
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

Merge user values with defaults before validation. #5623

Merged
merged 8 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion dashboard/src/actions/installedpackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ export function installPackage(
`The schema for this package is not valid. Please contact the package author. The following errors were found:\n${errorText}`,
);
}
const validation = validateValuesSchema(values, schema);
const validation = validateValuesSchema(
values,
schema,
availablePackageDetail.defaultValues,
);
if (!validation.valid) {
const errorText = validation?.errors
?.map(e => ` - ${e.instancePath}: ${e.message}`)
Expand Down
54 changes: 53 additions & 1 deletion dashboard/src/shared/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,67 @@ describe("validateValuesSchema", () => {
{
description: "Should validate a valid object",
values: "foo: bar\n",
defaultValues: "foo: default\n",
schema: {
properties: { foo: { type: "string" } },
},
valid: true,
errors: null,
},
{
description: "Should error if required value not provided",
values: "foo: bar\n",
defaultValues: "foo: default\n",
schema: {
required: ["bar"],
properties: {
foo: { type: "string" },
bar: { type: "string" },
},
},
valid: false,
errors: [
{
keyword: "required",
instancePath: "",
message: "must have required property 'bar'",
params: { missingProperty: "bar" },
schemaPath: "#/required",
},
],
},
{
description: "Should not error if required value has a default",
values: "foo: bar\n",
defaultValues: "foo: default\nbar: default\n",
schema: {
required: ["bar"],
properties: {
foo: { type: "string" },
bar: { type: "string" },
},
},
valid: true,
errors: null,
},
{
description: "Should not error if extra value not present in defaults is included",
values: "foo: bar\nelsewhere: bang",
defaultValues: "foo: default\nbar: default\n",
schema: {
required: ["bar"],
properties: {
foo: { type: "string" },
bar: { type: "string" },
},
},
valid: true,
errors: null,
},
{
description: "Should validate an invalid object",
values: "foo: bar\n",
defaultValues: "foo: 1\n",
schema: { properties: { foo: { type: "integer" } } },
valid: false,
errors: [
Expand All @@ -459,7 +511,7 @@ describe("validateValuesSchema", () => {
},
].forEach(t => {
it(t.description, () => {
const res = validateValuesSchema(t.values, t.schema);
const res = validateValuesSchema(t.values, t.schema, t.defaultValues);
expect(res.valid).toBe(t.valid);
expect(res.errors).toEqual(t.errors);
});
Expand Down
13 changes: 12 additions & 1 deletion dashboard/src/shared/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DeploymentEvent, IAjvValidateResult, IBasicFormParam } from "shared/typ
// TODO(agamez): check if we can replace this package by js-yaml or vice-versa
import YAML from "yaml";
import { getPathValueInYamlNode, getPathValueInYamlNodeWithDefault } from "./yamlUtils";
import * as jsonpatch from "fast-json-patch";

const ajv = new Ajv({ strict: false });

Expand Down Expand Up @@ -159,8 +160,18 @@ export function schemaToObject(schema?: string): JSONSchemaType<any> {
export function validateValuesSchema(
values: string,
schema: JSONSchemaType<any> | any,
defaultValues?: string,
): { valid: boolean; errors: ErrorObject[] | null | undefined } {
const valid = ajv.validate(schema, yaml.load(values));
let valuesToCheck = yaml.load(values);
if (defaultValues) {
const defaultYAML = yaml.load(defaultValues);
let patches = jsonpatch.compare(defaultYAML as Object, valuesToCheck as Object);
patches = patches.filter(function (d) {
return ["add", "replace"].includes(d.op);
});
valuesToCheck = jsonpatch.applyPatch(defaultYAML, patches).newDocument;
}
const valid = ajv.validate(schema, valuesToCheck);
return { valid: !!valid, errors: ajv.errors } as IAjvValidateResult;
}

Expand Down