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 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
12 changes: 10 additions & 2 deletions 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 Expand Up @@ -301,7 +305,11 @@ export function updateInstalledPackage(
`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
21 changes: 16 additions & 5 deletions dashboard/src/shared/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import Ajv, { ErrorObject, JSONSchemaType } from "ajv";
import { findIndex, isEmpty, set } from "lodash";
import { DeploymentEvent, IAjvValidateResult, IBasicFormParam } from "shared/types";
import YAML from "yaml";
import { getPathValueInYamlNode, getPathValueInYamlNodeWithDefault, parseToJS } from "./yamlUtils";
import * as jsonpatch from "fast-json-patch";
import { getPathValueInYamlNode, getPathValueInYamlNodeWithDefault } from "./yamlUtils";

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

Expand All @@ -22,7 +23,6 @@ export function retrieveBasicFormParams(
let params: IBasicFormParam[] = [];
if (schema?.properties && !isEmpty(schema.properties)) {
const properties = schema.properties;
const requiredProperties = schema.required;
const schemaExamples = schema.examples;
Object.keys(properties).forEach(propertyKey => {
const schemaProperty = properties[propertyKey] as JSONSchemaType<any>;
Expand Down Expand Up @@ -59,8 +59,9 @@ export function retrieveBasicFormParams(
: undefined,
// get the string values of the enum array
enum: schemaProperty?.enum?.map((item: { toString: () => any }) => item?.toString() ?? ""),
// check if the "required" array contains the current property
isRequired: requiredProperties?.includes(propertyKey),
// We leave the validation of user values to the Helm backend, since it will take
// into account default values (on install) and previously set values (on upgrade).
isRequired: false,
examples: examples,
// If exists, the value that is currently deployed
deployedValue: isLeaf
Expand Down Expand Up @@ -155,8 +156,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, parseToJS(values));
let valuesToCheck = YAML.parse(values);
if (defaultValues) {
const defaultYAML = YAML.parse(defaultValues);
let patches = jsonpatch.compare(defaultYAML as any, valuesToCheck as any);
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