Skip to content

Commit

Permalink
CNI - Allow ValidationMode definition for JSON forms (#263)
Browse files Browse the repository at this point in the history
* adds to standard config var

* CNI - Allow ValidationMode definition for JsonForms

---------

Co-authored-by: awburgard <austin.burgard@prismatic.io>
  • Loading branch information
pattra and awburgard authored Aug 27, 2024
1 parent f4b0253 commit aef6aae
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
51 changes: 50 additions & 1 deletion packages/spectral-test/src/ConfigVars.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type {
ConfigVar,
DataSourceConfigVar,
ConfigPages,
ConfigVars,
Connection,
} from "@prismatic-io/spectral";
import { ValueOf } from "@prismatic-io/spectral/dist/types/utils";
import { expectAssignable } from "tsd";
import { expectAssignable, expectNotType } from "tsd";

type RawConnectionElems = ValueOf<ConfigPages>["elements"];
expectAssignable<"A Connection" | "Ref Connection">(null as unknown as keyof RawConnectionElems);
Expand All @@ -16,6 +17,54 @@ expectAssignable<"A Connection" | "Ref Connection">(null as unknown as keyof Con
expectAssignable<Connection>(null as unknown as ConfigVars["A Connection"]);
expectAssignable<Connection>(null as unknown as ConfigVars["Ref Connection"]);

// Having both a dataSource & dataSourceType in a DataSourceConfigVar is invalid
expectNotType<DataSourceConfigVar>({
stableKey: "jsonFormDataSourceConfigVar",
dataSource: {
component: "example",
key: "jsonFormDataSource",
values: { bar: { configVar: "" } },
},
dataSourceType: "jsonForm",
perform: () =>
Promise.resolve({
result: { uiSchema: { type: "" }, schema: {}, data: {} },
}),
});

// validationMode should only be accepted by jsonForm and component reference config vars
expectAssignable<ConfigVar>({
stableKey: "jsonFormConfigVar",
dataType: "jsonForm",
validationMode: "ValidateAndHide",
});

expectAssignable<DataSourceConfigVar>({
stableKey: "jsonFormDataSourceConfigVar",
dataSourceType: "jsonForm",
perform: () =>
Promise.resolve({
result: { uiSchema: { type: "" }, schema: {}, data: {} },
}),
validationMode: "NoValidation",
});

expectAssignable<DataSourceConfigVar>({
stableKey: "componentRefConfigVar",
dataSource: {
component: "example",
key: "jsonFormDataSource",
values: { bar: { configVar: "" } },
},
validationMode: "NoValidation",
});

expectNotType<DataSourceConfigVar>({
dataSourceType: "boolean",
stableKey: "booleanConfigVar",
validationMode: "NoValidation",
});

// Subset of data source types support collections.
expectAssignable<DataSourceConfigVar>({
perform: async () => Promise.resolve({ result: ["string"] }),
Expand Down
2 changes: 1 addition & 1 deletion packages/spectral/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prismatic-io/spectral",
"version": "9.1.1",
"version": "9.1.2",
"description": "Utility library for building Prismatic components",
"keywords": ["prismatic"],
"main": "dist/index.js",
Expand Down
16 changes: 16 additions & 0 deletions packages/spectral/src/serverTypes/convertIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
CollectionType,
KeyValuePair,
ComponentManifest,
isJsonFormConfigVar,
isJsonFormDataSourceConfigVar,
} from "../types";
import {
Component as ServerComponent,
Expand Down Expand Up @@ -605,6 +607,13 @@ const convertConfigVar = (
result.scheduleType = "custom";
}

if (isJsonFormConfigVar(configVar) || isJsonFormDataSourceConfigVar(configVar)) {
result.meta = {
...result.meta,
validationMode: configVar?.validationMode ?? "ValidateAndShow",
};
}

if (isDataSourceDefinitionConfigVar(configVar)) {
result.dataType = configVar.dataSourceType;
result.dataSource = {
Expand All @@ -622,6 +631,13 @@ const convertConfigVar = (
result.dataType = componentRegistry[ref.component.key].dataSources[ref.key].dataSourceType;
result.dataSource = ref;
result.inputs = inputs;

if (configVar.validationMode) {
result.meta = {
...result.meta,
validationMode: configVar.validationMode,
};
}
}

return result;
Expand Down
33 changes: 28 additions & 5 deletions packages/spectral/src/types/ConfigVars.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ValidationMode } from "@jsonforms/core";
import {
type DataSourceDefinition,
type ConnectionDefinition,
Expand Down Expand Up @@ -183,7 +184,13 @@ type ObjectSelectionConfigVar = CreateStandardConfigVar<"objectSelection">;

type ObjectFieldMapConfigVar = CreateStandardConfigVar<"objectFieldMap">;

type JsonFormConfigVar = CreateStandardConfigVar<"jsonForm">;
type JsonFormConfigVar = CreateStandardConfigVar<"jsonForm"> & {
validationMode?: ValidationMode;
};

type JsonFormDataSourceDefinitionConfigVar = DataSourceDefinitionConfigVar & {
validationMode?: ValidationMode;
};

export type StandardConfigVar =
| StringConfigVar
Expand All @@ -206,10 +213,17 @@ type BaseDataSourceConfigVar<TDataSourceType extends DataSourceType = DataSource
collectionType?: CollectionType | undefined;
} & BaseConfigVar
: TDataSourceType extends Exclude<DataSourceType, CollectionDataSourceType>
? BaseConfigVar & {
dataSourceType: TDataSourceType;
collectionType?: undefined;
}
? TDataSourceType extends Extract<DataSourceType, "jsonForm">
? BaseConfigVar & {
dataSourceType: Extract<DataSourceType, "jsonForm">;
dataSource?: never;
collectionType?: undefined;
validationMode?: ValidationMode;
}
: BaseConfigVar & {
dataSourceType: TDataSourceType;
collectionType?: undefined;
}
:
| ({
dataSourceType: Extract<CollectionDataSourceType, TDataSourceType>;
Expand Down Expand Up @@ -237,6 +251,7 @@ type DataSourceReferenceConfigVar =
ComponentRegistryDataSource extends infer TDataSourceReference extends ComponentRegistryDataSource
? Omit<BaseDataSourceConfigVar<TDataSourceReference["dataSourceType"]>, "dataSourceType"> & {
dataSource: TDataSourceReference["reference"];
validationMode?: ValidationMode;
}
: never;

Expand Down Expand Up @@ -369,6 +384,14 @@ export const isCodeConfigVar = (cv: ConfigVar): cv is CodeConfigVar =>
export const isScheduleConfigVar = (cv: ConfigVar): cv is ScheduleConfigVar =>
"dataType" in cv && cv.dataType === "schedule";

export const isJsonFormConfigVar = (cv: ConfigVar): cv is JsonFormConfigVar =>
"dataType" in cv && cv.dataType === "jsonForm";

export const isJsonFormDataSourceConfigVar = (
cv: ConfigVar,
): cv is JsonFormDataSourceDefinitionConfigVar =>
"dataSourceType" in cv && cv.dataSourceType === "jsonForm";

export const isDataSourceDefinitionConfigVar = (
cv: ConfigVar,
): cv is DataSourceDefinitionConfigVar =>
Expand Down

0 comments on commit aef6aae

Please sign in to comment.