File tree Expand file tree Collapse file tree 3 files changed +21
-0
lines changed
Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -14,13 +14,16 @@ import {
1414 createSetFunction,
1515} from './utils/create-accessors';
1616import { createStore } from './utils/create-store';
17+ import { validateSchema } from './utils/validate-schema';
1718
1819let traitId = 0;
1920
2021function defineTrait<S extends Schema>(schema: S = {} as S): Trait<Norm<S>> {
2122 const isAoS = typeof schema === 'function';
2223 const traitType: TraitType = isAoS ? 'aos' : 'soa';
2324
25+ validateSchema(schema);
26+
2427 const Trait = Object.assign((params: Partial<Norm<S>>) => [Trait, params], {
2528 schema: schema as Norm<S>,
2629 [$internal]: {
Original file line number Diff line number Diff line change 1+ import { Schema } from '../types';
2+
3+ export function validateSchema(schema: Schema) {
4+ for (const key in schema) {
5+ const value = schema[key as keyof Schema];
6+ if (value !== null && typeof value === 'object') {
7+ const kind = Array.isArray(value) ? 'array' : 'object';
8+ throw new Error(`Koota: ${key} is an ${kind}, which is not supported in traits.`);
9+ }
10+ }
11+ }
Original file line number Diff line number Diff line change @@ -29,6 +29,13 @@ describe('Trait', () => {
2929 expect(typeof Test === 'function').toBe(true);
3030 });
3131
32+ it('should throw an error if the schema contains an object or array', () => {
33+ // @ts-expect-error - we want to test the error case
34+ expect(() => trait({ object: { a: 1, b: 2 } })).toThrow();
35+ // @ts-expect-error - we want to test the error case
36+ expect(() => trait({ array: [1, 2, 3] })).toThrow();
37+ });
38+
3239 it('should add and remove traits to an entity', () => {
3340 const entity = world.spawn();
3441
You can’t perform that action at this time.
0 commit comments