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 {
14
14
createSetFunction ,
15
15
} from './utils/create-accessors' ;
16
16
import { createStore } from './utils/create-store' ;
17
+ import { validateSchema } from './utils/validate-schema' ;
17
18
18
19
let traitId = 0 ;
19
20
20
21
function defineTrait < S extends Schema > ( schema : S = { } as S ) : Trait < Norm < S > > {
21
22
const isAoS = typeof schema === 'function' ;
22
23
const traitType : TraitType = isAoS ? 'aos' : 'soa' ;
23
24
25
+ validateSchema ( schema ) ;
26
+
24
27
const Trait = Object . assign ( ( params : Partial < Norm < S > > ) => [ Trait , params ] , {
25
28
schema : schema as Norm < S > ,
26
29
[ $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', () => {
29
29
expect ( typeof Test === 'function' ) . toBe ( true ) ;
30
30
} ) ;
31
31
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
+
32
39
it ( 'should add and remove traits to an entity' , ( ) => {
33
40
const entity = world . spawn ( ) ;
34
41
You can’t perform that action at this time.
0 commit comments