Skip to content

Commit

Permalink
feat: schema validator for struct type execution (#186)
Browse files Browse the repository at this point in the history
* feat: schema validator for struct type execution

Signed-off-by: Pianist038801 <[email protected]>

* fix: failing unit tests

Signed-off-by: Pianist038801 <[email protected]>

* fix: change schema form font size

Signed-off-by: Pianist038801 <[email protected]>

Co-authored-by: Pianist038801 <[email protected]>
  • Loading branch information
Pianist038801 and Pianist038801 authored Aug 24, 2021
1 parent d026108 commit 9744894
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 7 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
]
},
"dependencies": {
"@rjsf/core": "^3.0.0",
"@rjsf/material-ui": "^3.0.0",
"babel-polyfill": "^6.26.0",
"cache": "^2.1.0",
"chalk": "^2.0.1",
Expand Down
67 changes: 64 additions & 3 deletions src/components/Launch/LaunchForm/StructInput.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import { TextField } from '@material-ui/core';
import { TextField, Card, CardContent, CardHeader } from '@material-ui/core';
import * as React from 'react';
import { useState } from 'react';
import Form from '@rjsf/material-ui';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import { makeStringChangeHandler } from './handlers';
import { InputProps } from './types';
import { getLaunchInputId } from './utils';
import { protobufValueToPrimitive, PrimitiveType } from './inputHelpers/struct';

const muiTheme = createMuiTheme({
props: {
MuiTextField: {
variant: 'outlined'
}
}
});

muiTheme.typography.h5 = {
fontSize: '16px',
fontWeight: 400
};

/** Handles rendering of the input component for a Struct */
export const StructInput: React.FC<InputProps> = props => {
Expand All @@ -11,12 +28,56 @@ export const StructInput: React.FC<InputProps> = props => {
label,
name,
onChange,
typeDefinition: { subtype },
typeDefinition: { literalType },
value = ''
} = props;
const hasError = !!error;
const helperText = hasError ? error : props.helperText;
return (

const [paramData, setParamData] = useState({});
const onFormChange = ({ formData }, e) => {
onChange(JSON.stringify(formData));
setParamData(formData);
};

let jsonFormRenderable = false;
let parsedJson: PrimitiveType = {};

if (literalType?.metadata?.fields?.definitions?.structValue?.fields) {
const keys = Object.keys(
literalType?.metadata?.fields?.definitions?.structValue?.fields
);

if (keys[0]) {
parsedJson = protobufValueToPrimitive(
literalType.metadata.fields.definitions.structValue.fields[
`${keys[0]}`
]
);

if (parsedJson) jsonFormRenderable = true;
}
}

return jsonFormRenderable ? (
<MuiThemeProvider theme={muiTheme}>
<Card>
<CardHeader
title={label}
style={{ borderBottom: 'solid 1px gray' }}
/>
<CardContent>
<Form
schema={JSON.parse(JSON.stringify(parsedJson))}
formData={paramData}
onChange={onFormChange}
>
<div></div>
</Form>
</CardContent>
</Card>
</MuiThemeProvider>
) : (
<TextField
id={getLaunchInputId(name)}
error={hasError}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Launch/LaunchForm/inputHelpers/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { structPath } from './constants';
import { ConverterInput, InputHelper, InputValidatorParams } from './types';
import { extractLiteralWithCheck } from './utils';

type PrimitiveType = string | number | boolean | null | object;
export type PrimitiveType = string | number | boolean | null | object;

function asValueWithKind(value: Protobuf.IValue): Protobuf.Value {
return value instanceof Protobuf.Value
? value
: Protobuf.Value.create(value);
}

function protobufValueToPrimitive(
export function protobufValueToPrimitive(
value: Protobuf.IValue
): PrimitiveType | PrimitiveType[] {
const valueWithKind = asValueWithKind(value);
Expand Down
109 changes: 107 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,26 @@
prop-types "^15.6.1"
react-lifecycles-compat "^3.0.4"

"@rjsf/core@^3.0.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@rjsf/core/-/core-3.1.0.tgz#7667364a4532e25c164abbc2adabe39157ffec4c"
integrity sha512-EwM2juiQxEdXzFy9rIIsDr6/e+FYeR1cKx0/no8ASEuXZ1p+/nf/CxKGobzD6UhpRip7JK9PhhuHFEFBIjHFJA==
dependencies:
"@types/json-schema" "^7.0.7"
ajv "^6.7.0"
core-js-pure "^3.6.5"
json-schema-merge-allof "^0.6.0"
jsonpointer "^4.0.1"
lodash "^4.17.15"
nanoid "^3.1.23"
prop-types "^15.7.2"
react-is "^16.9.0"

"@rjsf/material-ui@^3.0.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@rjsf/material-ui/-/material-ui-3.1.0.tgz#ed15fc75de1594f87cd8336b7f2ebc492a432d8b"
integrity sha512-kWz37spT5SOXkb8Axq4g4BzQjXRylQr6B7eFAg1NPhSK7KrJYwSMSsFJ9Ze2vEBxwEiR0Z0n/4puaamGuGFRBw==

"@samverschueren/stream-to-observable@^0.3.0":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.1.tgz#a21117b19ee9be70c379ec1877537ef2e1c63301"
Expand Down Expand Up @@ -3818,6 +3838,11 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0"
integrity sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==

"@types/json-schema@^7.0.7":
version "7.0.9"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d"
integrity sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==

"@types/linkify-it@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@types/linkify-it/-/linkify-it-2.1.0.tgz#ea3dd64c4805597311790b61e872cbd1ed2cd806"
Expand Down Expand Up @@ -4646,7 +4671,7 @@ ajv-keywords@^3.1.0, ajv-keywords@^3.4.1, ajv-keywords@^3.5.2:
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-3.5.2.tgz#31f29da5ab6e00d1c2d329acf7b5929614d5014d"
integrity sha512-5p6WTN0DdTGVQk6VjcEju19IgaHudalcfabD7yhDGeA6bcQnmL+CpveLJq/3hvfwd1aof6L386Ougkx6RfyMIQ==

ajv@^6.1.0, ajv@^6.1.1, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.3, ajv@^6.12.4, ajv@^6.12.5:
ajv@^6.1.0, ajv@^6.1.1, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.3, ajv@^6.12.4, ajv@^6.12.5, ajv@^6.7.0:
version "6.12.6"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.6.tgz#baf5a62e802b07d977034586f8c3baf5adf26df4"
integrity sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==
Expand Down Expand Up @@ -6681,6 +6706,25 @@ compression-webpack-plugin@^3.0.1:
serialize-javascript "^2.1.2"
webpack-sources "^1.0.1"

compute-gcd@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/compute-gcd/-/compute-gcd-1.2.1.tgz#34d639f3825625e1357ce81f0e456a6249d8c77f"
integrity sha512-TwMbxBNz0l71+8Sc4czv13h4kEqnchV9igQZBi6QUaz09dnz13juGnnaWWJTRsP3brxOoxeB4SA2WELLw1hCtg==
dependencies:
validate.io-array "^1.0.3"
validate.io-function "^1.0.2"
validate.io-integer-array "^1.0.0"

compute-lcm@^1.1.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/compute-lcm/-/compute-lcm-1.1.2.tgz#9107c66b9dca28cefb22b4ab4545caac4034af23"
integrity sha512-OFNPdQAXnQhDSKioX8/XYT6sdUlXwpeMjfd6ApxMJfyZ4GxmLR1xvMERctlYhlHwIiz6CSpBc2+qYKjHGZw4TQ==
dependencies:
compute-gcd "^1.2.1"
validate.io-array "^1.0.3"
validate.io-function "^1.0.2"
validate.io-integer-array "^1.0.0"

compute-scroll-into-view@^1.0.14:
version "1.0.16"
resolved "https://registry.yarnpkg.com/compute-scroll-into-view/-/compute-scroll-into-view-1.0.16.tgz#5b7bf4f7127ea2c19b750353d7ce6776a90ee088"
Expand Down Expand Up @@ -6903,6 +6947,11 @@ core-js-pure@^3.0.0, core-js-pure@^3.0.1:
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.8.0.tgz#4cdd2eca37d49cda206b66e26204818dba77884a"
integrity sha512-fRjhg3NeouotRoIV0L1FdchA6CK7ZD+lyINyMoz19SyV+ROpC4noS1xItWHFtwZdlqfMfVPJEyEGdfri2bD1pA==

core-js-pure@^3.6.5:
version "3.16.2"
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.16.2.tgz#0ef4b79cabafb251ea86eb7d139b42bd98c533e8"
integrity sha512-oxKe64UH049mJqrKkynWp6Vu0Rlm/BTXO/bJZuN2mmR3RtOFNepLlSWDd1eo16PzHpQAoNG97rLU1V/YxesJjw==

core-js@^1.0.0:
version "1.2.7"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-1.2.7.tgz#652294c14651db28fa93bd2d5ff2983a4f08c636"
Expand Down Expand Up @@ -11756,6 +11805,22 @@ json-parse-even-better-errors@^2.3.0:
resolved "https://registry.yarnpkg.com/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz#7c47805a94319928e05777405dc12e1f7a4ee02d"
integrity sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==

json-schema-compare@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/json-schema-compare/-/json-schema-compare-0.2.2.tgz#dd601508335a90c7f4cfadb6b2e397225c908e56"
integrity sha512-c4WYmDKyJXhs7WWvAWm3uIYnfyWFoIp+JEoX34rctVvEkMYCPGhXtvmFFXiffBbxfZsvQ0RNnV5H7GvDF5HCqQ==
dependencies:
lodash "^4.17.4"

json-schema-merge-allof@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/json-schema-merge-allof/-/json-schema-merge-allof-0.6.0.tgz#64d48820fec26b228db837475ce3338936bf59a5"
integrity sha512-LEw4VMQVRceOPLuGRWcxW5orTTiR9ZAtqTAe4rQUjNADTeR81bezBVFa0MqIwp0YmHIM1KkhSjZM7o+IQhaPbQ==
dependencies:
compute-lcm "^1.1.0"
json-schema-compare "^0.2.2"
lodash "^4.17.4"

json-schema-traverse@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660"
Expand Down Expand Up @@ -11828,6 +11893,11 @@ jsonparse@^1.2.0:
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
integrity sha1-P02uSpH6wxX3EGL4UhzCOfE2YoA=

jsonpointer@^4.0.1:
version "4.1.0"
resolved "https://registry.yarnpkg.com/jsonpointer/-/jsonpointer-4.1.0.tgz#501fb89986a2389765ba09e6053299ceb4f2c2cc"
integrity sha512-CXcRvMyTlnR53xMcKnuMzfCA5i/nfblTnnr74CZb6C4vG39eu6w51t7nKmU5MfLfbTgGItliNyjO/ciNPDqClg==

jsontoxml@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/jsontoxml/-/jsontoxml-1.0.1.tgz#07fff7f6bfbfa1097d779aec7f041b5046075e70"
Expand Down Expand Up @@ -13333,6 +13403,11 @@ nan@^2.14.0:
resolved "https://registry.yarnpkg.com/nan/-/nan-2.15.0.tgz#3f34a473ff18e15c1b5626b62903b5ad6e665fee"
integrity sha512-8ZtvEnA2c5aYCZYd1cvgdnU6cqwixRoYg70xPLWUws5ORTa/lnw+u4amixRS/Ac5U5mQVgp9pnlSUnbNWFaWZQ==

nanoid@^3.1.23:
version "3.1.25"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.25.tgz#09ca32747c0e543f0e1814b7d3793477f9c8e152"
integrity sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q==

napi-build-utils@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/napi-build-utils/-/napi-build-utils-1.0.2.tgz#b1fddc0b2c46e380a0b7a76f984dd47c41a13806"
Expand Down Expand Up @@ -15508,7 +15583,7 @@ react-intersection-observer@^8.25.1:
resolved "https://registry.yarnpkg.com/react-intersection-observer/-/react-intersection-observer-8.31.0.tgz#0ed21aaf93c4c0475b22b0ccaba6169076d01605"
integrity sha512-XraIC/tkrD9JtrmVA7ypEN1QIpKc52mXBH1u/bz/aicRLo8QQEJQAMUTb8mz4B6dqpPwyzgjrr7Ljv/2ACDtqw==

react-is@^16.13.1, react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.0, react-is@^16.8.1, react-is@^16.8.6:
react-is@^16.13.1, react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.0, react-is@^16.8.1, react-is@^16.8.6, react-is@^16.9.0:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
Expand Down Expand Up @@ -18661,6 +18736,36 @@ validate-npm-package-name@^3.0.0, validate-npm-package-name@~3.0.0:
dependencies:
builtins "^1.0.3"

validate.io-array@^1.0.3:
version "1.0.6"
resolved "https://registry.yarnpkg.com/validate.io-array/-/validate.io-array-1.0.6.tgz#5b5a2cafd8f8b85abb2f886ba153f2d93a27774d"
integrity sha1-W1osr9j4uFq7L4hroVPy2Tond00=

validate.io-function@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/validate.io-function/-/validate.io-function-1.0.2.tgz#343a19802ed3b1968269c780e558e93411c0bad7"
integrity sha1-NDoZgC7TsZaCaceA5VjpNBHAutc=

validate.io-integer-array@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/validate.io-integer-array/-/validate.io-integer-array-1.0.0.tgz#2cabde033293a6bcbe063feafe91eaf46b13a089"
integrity sha1-LKveAzKTpry+Bj/q/pHq9GsToIk=
dependencies:
validate.io-array "^1.0.3"
validate.io-integer "^1.0.4"

validate.io-integer@^1.0.4:
version "1.0.5"
resolved "https://registry.yarnpkg.com/validate.io-integer/-/validate.io-integer-1.0.5.tgz#168496480b95be2247ec443f2233de4f89878068"
integrity sha1-FoSWSAuVviJH7EQ/IjPeT4mHgGg=
dependencies:
validate.io-number "^1.0.3"

validate.io-number@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/validate.io-number/-/validate.io-number-1.0.3.tgz#f63ffeda248bf28a67a8d48e0e3b461a1665baf8"
integrity sha1-9j/+2iSL8opnqNSODjtGGhZluvg=

value-equal@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/value-equal/-/value-equal-1.0.1.tgz#1e0b794c734c5c0cade179c437d356d931a34d6c"
Expand Down

0 comments on commit 9744894

Please sign in to comment.