Merge pull request #16227 from dagda1/fix-stepper-has-errors

fix stepper hasErrors to not bottom out at first error
This commit is contained in:
Ben Lambert
2023-02-07 12:54:50 +01:00
committed by GitHub
3 changed files with 99 additions and 1 deletions
@@ -181,6 +181,18 @@ describe('createAsyncValidators', () => {
title: 'General',
type: 'object',
properties: {
address: {
type: 'object',
'ui:field': 'AddressField',
properties: {
street: {
type: 'string',
},
postcode: {
type: 'string',
},
},
},
name: {
title: 'Name',
type: 'string',
@@ -207,6 +219,19 @@ describe('createAsyncValidators', () => {
},
};
const AddressField: NextCustomFieldValidator<{
street?: string;
postcode?: string;
}> = (value, { addError }) => {
if (!value.postcode) {
addError('postcode is missing!');
}
if (!value.street) {
addError('street is missing here!');
}
};
const NameField: NextCustomFieldValidator<string> = (
value,
{ addError },
@@ -217,6 +242,7 @@ describe('createAsyncValidators', () => {
};
const validators = {
AddressField: AddressField as NextCustomFieldValidator<unknown>,
NameField: NameField as NextCustomFieldValidator<unknown>,
};
@@ -228,11 +254,18 @@ describe('createAsyncValidators', () => {
validate({
actionType: 'newThing',
general: {
address: {
street: 'street',
postcode: 'postcode',
},
name: undefined,
},
}),
).resolves.toEqual({
general: {
address: expect.objectContaining({
__errors: [],
}),
name: expect.objectContaining({
__errors: ['something is broken here!'],
}),
@@ -0,0 +1,61 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { hasErrors } from './utils';
describe('hasErrors', () => {
it('should return false for empty _errors', () => {
expect(
hasErrors({
name: {
__errors: [],
addError: jest.fn(),
},
}),
).toBe(false);
});
it('should return true for a single error', () => {
expect(
hasErrors({
name: {
__errors: ['an error'],
addError: jest.fn(),
},
}),
).toBe(true);
});
it('should return true for more than one error', () => {
expect(
hasErrors({
name: {
__errors: [],
addError: jest.fn(),
},
general: {
address: {
__errors: [],
addError: jest.fn(),
},
name: {
__errors: ['something is broken here!'],
addError: jest.fn(),
},
},
}),
).toBe(true);
});
});
@@ -28,7 +28,11 @@ export function hasErrors(errors?: FormValidation): boolean {
for (const error of Object.values(errors)) {
if (isFieldValidation(error)) {
return (error.__errors ?? []).length > 0;
if ((error.__errors ?? []).length > 0) {
return true;
}
continue;
}
return hasErrors(error);