Skip to content

Commit 072bd91

Browse files
authored
fix: Move validate-props method to component-toolkit package (#150)
1 parent dfbb81d commit 072bd91

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { validateProps } from '../validate-props';
5+
import { isDevelopment } from '../../is-development';
6+
7+
jest.mock('../../is-development', () => ({ isDevelopment: false }));
8+
9+
test('does nothing in production builds', () => {
10+
expect(isDevelopment).toBe(false);
11+
expect(() => validateProps('TestComponent', { variant: 'foo' }, ['variant'], {}, 'default')).not.toThrow();
12+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { validateProps } from '../validate-props';
5+
6+
test('should pass validation', () => {
7+
expect(() => validateProps('TestComponent', {}, [], {}, 'default')).not.toThrow();
8+
expect(() =>
9+
validateProps('TestComponent', { variant: 'foo' }, ['bar'], { variant: ['foo'] }, 'default')
10+
).not.toThrow();
11+
expect(() =>
12+
validateProps('TestComponent', { variant: undefined }, ['bar'], { variant: ['foo'] }, 'default')
13+
).not.toThrow();
14+
});
15+
16+
test('should throw error when excluded prop is used', () => {
17+
expect(() => validateProps('TestComponent', { variant: 'foo' }, ['variant'], {}, 'default')).toThrow(
18+
new Error('TestComponent does not support "variant" property when used in default system')
19+
);
20+
});
21+
22+
test('should throw error when invalid prop is used', () => {
23+
expect(() => validateProps('TestComponent', { variant: 'foo' }, [], { variant: ['bar'] }, 'default')).toThrow(
24+
new Error('TestComponent does not support "variant" with value "foo" when used in default system')
25+
);
26+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { isDevelopment } from '../is-development';
5+
6+
export function validateProps(
7+
componentName: string,
8+
props: Record<string, any>,
9+
excludedProps: Array<string>,
10+
allowedEnums: Record<string, Array<string>>,
11+
systemName: string
12+
) {
13+
if (!isDevelopment) {
14+
return;
15+
}
16+
for (const [prop, value] of Object.entries(props)) {
17+
if (excludedProps.includes(prop)) {
18+
throw new Error(`${componentName} does not support "${prop}" property when used in ${systemName} system`);
19+
}
20+
if (value && allowedEnums[prop] && !allowedEnums[prop].includes(value)) {
21+
throw new Error(
22+
`${componentName} does not support "${prop}" with value "${value}" when used in ${systemName} system`
23+
);
24+
}
25+
}
26+
}

src/internal/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ export { default as circleIndex } from './utils/circle-index';
3939
export { default as Portal, PortalProps } from './portal';
4040
export { useMergeRefs } from './use-merge-refs';
4141
export { useRandomId, useUniqueId } from './use-unique-id';
42+
export { validateProps } from './base-component/validate-props';

0 commit comments

Comments
 (0)