Skip to content

Commit

Permalink
chore(core): fix JsonObject type doesn't allow array of JsonObject as…
Browse files Browse the repository at this point in the history
… field (#13669)

* chore(core): fix JsonObject type doesn't allow array of JsonObject as field

* chore: fix an array of types

* chore: allow array of JsonArray

* chore: refine the test
  • Loading branch information
HuiSF authored Aug 7, 2024
1 parent 28434a6 commit d7522e4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
58 changes: 58 additions & 0 deletions packages/core/__tests__/singleton/Auth/type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { JWT } from '../../../src/singleton/Auth/types';

describe('type validity', () => {
describe('JWT type', () => {
it('can contain property that has a value as array of JsonObjects', () => {
type OtherProperty1 = (
| { key: string }
| number
| string
| (
| { key: string }
| number
| string
| ({ key: string } | number | string)[]
)[]
)[];
// For testing purpose, use type alias here, as TS will complain while using
// an interface which triggers structural typing check
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
type OtherProperty2 = {
key: number;
array: (
| { key: string }
| number
| string
| ({ key: string } | number | string)[]
)[];
};
const expectedOtherProperty1 = [
{ key: '123' },
1,
'hi',
[1, 'hi', { key: '345' }, [2, 'hi', { key: '456' }]],
];
const expectedOtherProperty2 = {
key: 1,
array: [1, 'hi', { key: '123' }, [2, 'hi', { key: '456' }]],
};
const value: JWT = {
payload: {
otherProperty1: expectedOtherProperty1,
otherProperty2: expectedOtherProperty2,
},
toString: () => 'mock',
};

const extractedOtherProperty1 = value.payload
.otherProperty1 as OtherProperty1;
const a: OtherProperty1 = extractedOtherProperty1;
expect(a).toEqual(expectedOtherProperty1);

const extractedOtherProperty2 = value.payload
.otherProperty2 as OtherProperty2;
const b: OtherProperty2 = extractedOtherProperty2;
expect(b).toEqual(expectedOtherProperty2);
});
});
});
2 changes: 1 addition & 1 deletion packages/core/src/singleton/Auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface JwtPayloadStandardFields {
type JsonPrimitive = null | string | number | boolean;

/** JSON array type */
type JsonArray = JsonPrimitive[];
type JsonArray = (JsonPrimitive | JsonObject | JsonArray)[];

/** JSON Object type */
interface JsonObject {
Expand Down

0 comments on commit d7522e4

Please sign in to comment.