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

Feat: Pass validation severity into UI #416

Merged
merged 3 commits into from
Mar 27, 2024
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
24 changes: 20 additions & 4 deletions src/lib/components/controls/ValidationErrorsOverview.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
.jse-validation-errors-overview {
font-family: $font-family-mono;
font-size: $font-size-mono;
background: $message-warning-background;
color: $message-warning-color;
overflow: auto;
max-height: $errors-overview-max-height;

Expand All @@ -15,8 +13,26 @@
tr {
cursor: pointer;

&.jse-validation-error {
background: $message-error-background;
color: $message-error-color;
}

&.jse-validation-warning {
background: $message-warning-background;
color: $message-warning-color;
&:hover {
filter: brightness(105%);
}
}

&.jse-validation-info {
background: $message-info-background;
color: $message-info-color;
}

&:hover {
background-color: rgba(255, 255, 255, 0.1);
filter: brightness(110%);
josdejong marked this conversation as resolved.
Show resolved Hide resolved
}

td {
Expand Down Expand Up @@ -55,4 +71,4 @@
}
}
}
}
}
17 changes: 14 additions & 3 deletions src/lib/components/controls/ValidationErrorsOverview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { isEmpty } from 'lodash-es'
import Icon from 'svelte-awesome'
import { stringifyJSONPath } from '$lib/utils/pathUtils.js'
import type { ValidationError } from '$lib/types.js'
import { ValidationSeverity, type ValidationError } from '$lib/types.js'
import { MAX_VALIDATION_ERRORS } from '$lib/constants.js'
import { limit } from '$lib/utils/arrayUtils.js'

Expand All @@ -27,6 +27,17 @@
function expand() {
expanded = true
}

function getValidationClass(errors: ValidationError[]): string {
if (errors.some(e => e.severity === ValidationSeverity.error)) {
return 'error';
} else if (errors.some(e => e.severity === ValidationSeverity.warning)) {
return 'warning';
} else if (errors.some(e => e.severity === ValidationSeverity.info)) {
return 'info';
}
return '';
}
</script>

{#if !isEmpty(validationErrors)}
Expand All @@ -36,7 +47,7 @@
<tbody>
{#each limit(validationErrors, MAX_VALIDATION_ERRORS) as validationError, index}
<tr
class="jse-validation-error"
class="jse-validation-{validationError.severity}"
on:click={() => {
// trigger on the next tick to prevent the editor not getting focus
setTimeout(() => selectError(validationError))
Expand Down Expand Up @@ -79,7 +90,7 @@
{:else}
<table class="jse-validation-errors-overview-collapsed">
<tbody>
<tr class="jse-validation-error" on:click={expand}>
<tr class="jse-validation-{getValidationClass(validationErrors)}" on:click={expand}>
<td class="jse-validation-error-icon">
<Icon data={faExclamationTriangle} />
</td>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/modes/textmode/TextMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@
}

function toRichValidationError(validationError: ValidationError): RichValidationError {
const { path, message } = validationError
const { path, message, severity } = validationError
const { line, column, from, to } = findTextLocation(normalization.escapeValue(text), path)

return {
Expand All @@ -643,7 +643,7 @@
from,
to,
message,
severity: ValidationSeverity.warning,
severity,
actions: []
}
}
Expand Down
24 changes: 23 additions & 1 deletion src/lib/components/modes/treemode/ValidationErrorIcon.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,27 @@ button.jse-validation-error {
vertical-align: top;
display: inline-flex;

color: $warning-color;
color: $error-color;
}

button.jse-validation-info {
@include jsoneditor-button;

padding: 0;
margin: 0;
vertical-align: top;
display: inline-flex;

color: $info-color;
}

button.jse-validation-warning {
@include jsoneditor-button;

padding: 0;
margin: 0;
vertical-align: top;
display: inline-flex;

color: $warning-color;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<button
type="button"
class="jse-validation-error"
class="jse-validation-{validationError.severity}"
on:click={onExpand}
use:tooltip={{ text, ...absolutePopupContext }}
>
Expand Down
1 change: 1 addition & 0 deletions src/lib/themes/defaults.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ $text-readonly: var(--jse-text-readonly, #8d8d8d);
$text-color-inverse: var(--jse-text-color-inverse, $white);
$error-color: var(--jse-error-color, #ee5341);
$warning-color: var(--jse-warning-color, #fdc539);
$info-color: var(--jse-info-color, #4f91ff);

/* main, menu, modal */
$main-border: var(--jse-main-border, (1px solid #d7d7d7));
Expand Down
4 changes: 2 additions & 2 deletions src/routes/examples/custom_validation/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

// check whether the team consists of exactly four members
if (json.team.length !== 4) {
errors.push({ path: ['team'], message: 'A team must have 4 members', severity: 'warning' })
errors.push({ path: ['team'], message: 'A team must have 4 members', severity: 'error' })
}

// check whether there is at least one adult member in the team
Expand All @@ -67,7 +67,7 @@
errors.push({
path: ['team'],
message: 'A team must have at least one adult person (age >= 18)',
severity: 'warning'
severity: 'info'
})
}
} else {
Expand Down
Loading