-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using Ajv properly, with basic json stringify and parse
Signed-off-by: Aaron Chong <[email protected]>
- Loading branch information
1 parent
440dc2c
commit 0ec29e0
Showing
11 changed files
with
137 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/react-components/lib/tasks/task-request-label-utils.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Ajv from 'ajv'; | ||
import schema from 'api-client/dist/schema'; | ||
import type { TaskState, TaskRequestLabel } from 'api-client'; | ||
|
||
// FIXME: AJV is duplicated here with dashboard, but this is only a temporary | ||
// measure until we start using an external validation tool. | ||
const ajv = new Ajv(); | ||
|
||
Object.entries(schema.components.schemas).forEach(([k, v]) => { | ||
ajv.addSchema(v, `#/components/schemas/${k}`); | ||
}); | ||
|
||
const validateTaskRequestLabel = ajv.compile(schema.components.schemas.TaskRequestLabel); | ||
|
||
export function serializeTaskRequestLabel(label: TaskRequestLabel): string { | ||
return JSON.stringify(label); | ||
} | ||
|
||
export function getTaskRequestLabelFromJsonString( | ||
jsonString: string, | ||
): TaskRequestLabel | undefined { | ||
try { | ||
// Validate first before parsing again into the interface | ||
const validated = validateTaskRequestLabel(JSON.parse(jsonString)); | ||
if (validated) { | ||
const parsedLabel: TaskRequestLabel = JSON.parse(jsonString); | ||
return parsedLabel; | ||
} | ||
} catch (e) { | ||
console.error(`Failed to parse TaskRequestLabel: ${(e as Error).message}`); | ||
return undefined; | ||
} | ||
|
||
console.error(`Failed to validate TaskRequestLabel`); | ||
return undefined; | ||
} | ||
|
||
export function getTaskRequestLabelFromTaskState(taskState: TaskState): TaskRequestLabel | null { | ||
let requestLabel: TaskRequestLabel | null = null; | ||
if (taskState.booking.labels) { | ||
for (const label of taskState.booking.labels) { | ||
try { | ||
const parsedLabel = getTaskRequestLabelFromJsonString(label); | ||
if (parsedLabel) { | ||
requestLabel = parsedLabel; | ||
break; | ||
} | ||
} catch (e) { | ||
continue; | ||
} | ||
} | ||
} | ||
return requestLabel; | ||
} |
13 changes: 0 additions & 13 deletions
13
packages/react-components/lib/tasks/task-request-label.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.