Skip to content

Commit

Permalink
Merge branch 'bl-nero/validation' into bl-nero/tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
bl-nero committed Dec 3, 2024
2 parents 0f803c3 + 22be016 commit afd0cf3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ import FieldInput from '../FieldInput';

type StringListValidationResult = ValidationResult & {
/**
* A list of validation results, one per label. Note: items are optional just
* because `useRule` by default returns only `ValidationResult`. For the
* actual validation, it's not optional; if it's undefined, or there are
* fewer items in this list than the labels, the corresponding items will be
* treated as valid.
* A list of validation results, one per list item. Note: results are
* optional just because `useRule` by default returns only
* `ValidationResult`. For the actual validation, it's not optional; if it's
* undefined, or there are fewer results in this list than the list items,
* the corresponding items will be treated as valid.
*/
items?: ValidationResult[];
results?: ValidationResult[];
};

export type FieldMultiInputProps = {
Expand Down Expand Up @@ -119,7 +119,7 @@ export function FieldMultiInput({
<FieldInput
value={val}
rule={precomputed(
validationResult.items?.[i] ?? { valid: true }
validationResult.results?.[i] ?? { valid: true }
)}
ref={toFocus.current === i ? setFocus : null}
onChange={e =>
Expand Down
6 changes: 4 additions & 2 deletions web/packages/shared/components/Validation/Validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ export default class Validator extends Store<ValidatorState> {

const ValidationContext = React.createContext<Validator | undefined>(undefined);

type ValidationRenderFunction = (arg: { validator: Validator }) => any;
type ValidationRenderFunction = (arg: {
validator: Validator;
}) => React.ReactNode;

/**
* Installs a validation context that provides a {@link Validator} store. The
Expand Down Expand Up @@ -184,7 +186,7 @@ export function Validation(props: {
export function useValidation(): Validator | undefined {
const validator = React.useContext(ValidationContext);
if (!validator) {
logger.warn('Missing Validation Context declaration');
throw new Error('useValidation() called without a validation context');
}
return useStore(validator);
}
4 changes: 2 additions & 2 deletions web/packages/shared/components/Validation/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ test.each([
items: ['a', '', 'c'],
expected: {
valid: false,
items: [
results: [
{ valid: true, message: '' },
{ valid: false, message: 'required' },
{ valid: true, message: '' },
Expand All @@ -202,7 +202,7 @@ test.each([
items: ['a', 'b', 'c'],
expected: {
valid: true,
items: [
results: [
{ valid: true, message: '' },
{ valid: true, message: '' },
{ valid: true, message: '' },
Expand Down
4 changes: 2 additions & 2 deletions web/packages/shared/components/Validation/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ const requiredAll =
/** A result of the {@link arrayOf} validation rule. */
export type ArrayValidationResult<R = ValidationResult> = ValidationResult & {
/** Results of validating each separate item. */
items: R[];
results: R[];
};

/** Validates an array by executing given rule on each of its elements. */
Expand All @@ -296,7 +296,7 @@ const arrayOf =
(values: T[]) =>
() => {
const results = values.map(v => elementRule(v)());
return { items: results, valid: results.every(r => r.valid) };
return { results: results, valid: results.every(r => r.valid) };
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe('validation rules', () => {
}));
return {
valid: results.every(r => r.name.valid && r.value.valid),
items: results,
results: results,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type LabelListValidationResult = ValidationResult & {
* fewer items in this list than the labels, a default validation rule will
* be used instead.
*/
items?: LabelValidationResult[];
results?: LabelValidationResult[];
};

type LabelValidationResult = {
Expand Down Expand Up @@ -154,7 +154,7 @@ export function LabelsInput({
<Box>
{labels.map((label, index) => {
const validationItem: LabelValidationResult | undefined =
validationResult.items?.[index];
validationResult.results?.[index];
return (
<Box mb={2} key={index}>
<Flex alignItems="start">
Expand Down Expand Up @@ -246,6 +246,6 @@ export const nonEmptyLabels: LabelsRule = labels => () => {
}));
return {
valid: results.every(r => r.name.valid && r.value.valid),
items: results,
results: results,
};
};

0 comments on commit afd0cf3

Please sign in to comment.