Skip to content

Commit

Permalink
feat(core/forms): Remove all async support from the spinnaker validat…
Browse files Browse the repository at this point in the history
…ion apis (#7435)

After discussions with Alan Quach, we decided that async validation can
be externalized, and async support in this API reduced the ability for
developers to debug their form validations.  We also noted that we were
not using async validation anywhere in Deck.
  • Loading branch information
christopherthielen committed Sep 25, 2019
1 parent bfa0d17 commit 6ccc8cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 487 deletions.
13 changes: 4 additions & 9 deletions app/scripts/modules/core/src/presentation/forms/FormField.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import * as React from 'react';
import { isNil } from 'lodash';
import { IPromise } from 'angular';
import { $q } from 'ngimport';

import { noop } from 'core/utils';

import { LayoutContext } from './layouts';
import { useLatestPromise } from '../hooks';
import { createFieldValidator } from './FormikFormField';
import { renderContent } from './fields/renderContent';
import { IValidator, IValidatorResultRaw } from './validation';
import { IValidator } from './validation';
import {
ICommonFormFieldProps,
IControlledInputProps,
Expand Down Expand Up @@ -49,17 +46,15 @@ export function FormField(props: IFormFieldProps) {
const addValidator = useCallback((v: IValidator) => setInternalValidators(list => list.concat(v)), []);
const removeValidator = useCallback((v: IValidator) => setInternalValidators(list => list.filter(x => x !== v)), []);

// Capture props.validate when the component initializes (to allow for inline arrow functions)
const validate = useMemo(() => props.validate, []);

const fieldValidator = useMemo(
() => createFieldValidator(label, required, [].concat(validate || noop).concat(internalValidators)),
[label, required, validate],
);

const { result: errorMessage } = useLatestPromise(
// TODO: remove the following cast when we remove async validation from our API
() => $q.resolve((fieldValidator(value) as any) as IPromise<IValidatorResultRaw>),
[fieldValidator, value],
);
const errorMessage = useMemo(() => fieldValidator(value), [fieldValidator, value]);

const validationMessage = firstDefined(messageProp, errorMessage ? errorMessage : undefined);
const validationStatus = firstDefined(statusProp, errorMessage ? 'error' : undefined);
Expand Down
Loading

0 comments on commit 6ccc8cd

Please sign in to comment.