Skip to content

Commit

Permalink
Address review
Browse files Browse the repository at this point in the history
  • Loading branch information
somebody1234 committed Feb 26, 2024
1 parent f590db3 commit 01379a0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const DEFS: Record<string, object> = SCHEMA.$defs
const AJV = new Ajv({ formats: { 'enso-secret': true } })
AJV.addSchema(SCHEMA)

// =================
// === getSchema ===
// =================

/** Get a known schema using a path.
* @throws {Error} when there is no schema present at the given path. */
function getSchema(path: string) {
return error.assert<ajv.ValidateFunction>(() => AJV.getSchema(path))
}

// =====================
// === constantValue ===
// =====================
Expand Down Expand Up @@ -156,9 +166,7 @@ export default function DataLinkInput(props: DataLinkInputProps) {
value={typeof value === 'string' ? value : ''}
size={1}
className={`rounded-full w-40 px-2 bg-transparent border leading-170 h-6 py-px disabled:opacity-50 read-only:opacity-75 read-only:cursor-not-allowed ${
error.assert<ajv.ValidateFunction>(() => AJV.getSchema(path))(value)
? 'border-black/10'
: 'border-red-700/60'
getSchema(path)(value) ? 'border-black/10' : 'border-red-700/60'
}`}
placeholder="Enter text"
onChange={event => {
Expand All @@ -177,9 +185,7 @@ export default function DataLinkInput(props: DataLinkInputProps) {
value={typeof value === 'number' ? value : ''}
size={1}
className={`rounded-full w-40 px-2 bg-transparent border leading-170 h-6 py-px disabled:opacity-50 read-only:opacity-75 read-only:cursor-not-allowed ${
error.assert<ajv.ValidateFunction>(() => AJV.getSchema(path))(value)
? 'border-black/10'
: 'border-red-700/60'
getSchema(path)(value) ? 'border-black/10' : 'border-red-700/60'
}`}
placeholder="Enter number"
onChange={event => {
Expand All @@ -199,9 +205,7 @@ export default function DataLinkInput(props: DataLinkInputProps) {
value={typeof value === 'number' ? value : ''}
size={1}
className={`rounded-full w-40 px-2 bg-transparent border leading-170 h-6 py-px disabled:opacity-50 read-only:opacity-75 read-only:cursor-not-allowed ${
error.assert<ajv.ValidateFunction>(() => AJV.getSchema(path))(value)
? 'border-black/10'
: 'border-red-700/60'
getSchema(path)(value) ? 'border-black/10' : 'border-red-700/60'
}`}
placeholder="Enter integer"
onChange={event => {
Expand Down Expand Up @@ -336,12 +340,10 @@ export default function DataLinkInput(props: DataLinkInputProps) {
const childValue = selectedChildSchema == null ? [] : constantValue(selectedChildSchema)
if (
value != null &&
(selectedChildSchema == null ||
error.assert<ajv.ValidateFunction>(() => AJV.getSchema(selectedChildPath))(value) !==
true)
(selectedChildSchema == null || getSchema(selectedChildPath)(value) !== true)
) {
const newIndexRaw = childSchemas.findIndex((_, index) =>
error.assert<ajv.ValidateFunction>(() => AJV.getSchema(`${path}/anyOf/${index}`))(value)
getSchema(`${path}/anyOf/${index}`)(value)
)
const newIndex = selectedChildSchema == null && newIndexRaw === -1 ? 0 : newIndexRaw
if (newIndex !== -1 && newIndex !== selectedChildIndex) {
Expand Down
4 changes: 2 additions & 2 deletions app/ide-desktop/lib/dashboard/src/utilities/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export function assert<T>(makeValue: () => T | '' | 0 | 0n | false | null | unde
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!result) {
throw new Error(
'Value should not be null: `' +
'Assertion failed: `' +
makeValue.toString().replace(/^\s*[(].*?[)]\s*=>\s*/, '') +
'`'
'` should not be `null`.'
)
} else {
return result
Expand Down

0 comments on commit 01379a0

Please sign in to comment.