Skip to content

Commit 30b3cd7

Browse files
committed
[EBT] Fix schema support to Date
1 parent 63f2d66 commit 30b3cd7

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

packages/analytics/client/src/schema/types.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,66 @@ describe('schema types', () => {
182182
});
183183
});
184184

185+
describe('Date value', () => {
186+
test('it should allow the correct type and enforce the _meta.description', () => {
187+
let valueType: SchemaValue<Date> = {
188+
type: 'date',
189+
_meta: {
190+
description: 'Some description',
191+
},
192+
};
193+
194+
valueType = {
195+
type: 'keyword',
196+
_meta: {
197+
description: 'Some description',
198+
optional: false,
199+
},
200+
};
201+
202+
valueType = {
203+
// @ts-expect-error because the type does not match
204+
type: 'long',
205+
_meta: {
206+
description: 'Some description',
207+
optional: false,
208+
},
209+
};
210+
211+
valueType = {
212+
type: 'keyword',
213+
_meta: {
214+
description: 'Some description',
215+
// @ts-expect-error optional can't be true when the types don't set the value as optional
216+
optional: true,
217+
},
218+
};
219+
220+
// @ts-expect-error because it's missing the _meta.description
221+
valueType = { type: 'date' };
222+
expect(valueType).not.toBeUndefined(); // <-- Only to stop the var-not-used complain
223+
});
224+
test('it should enforce `_meta.optional: true`', () => {
225+
let valueType: SchemaValue<Date | undefined> = {
226+
type: 'date',
227+
_meta: {
228+
description: 'Some description',
229+
optional: true,
230+
},
231+
};
232+
233+
valueType = {
234+
type: 'date',
235+
_meta: {
236+
description: 'Some description',
237+
// @ts-expect-error because optional can't be false when the value can be undefined
238+
optional: false,
239+
},
240+
};
241+
expect(valueType).not.toBeUndefined(); // <-- Only to stop the var-not-used complain
242+
});
243+
});
244+
185245
describe('Object value', () => {
186246
test('it should allow "pass_through" and enforce the _meta.description', () => {
187247
let valueType: SchemaValue<{ a_value: string }> = {

packages/analytics/client/src/schema/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export type AllowedSchemaTypes =
3131
/**
3232
* Helper to ensure the declared types match the schema types
3333
*/
34-
export type PossibleSchemaTypes<Value> = Value extends string
34+
export type PossibleSchemaTypes<Value> = Value extends string | Date
3535
? AllowedSchemaStringTypes
3636
: Value extends number
3737
? AllowedSchemaNumberTypes
@@ -66,6 +66,8 @@ export type SchemaValue<Value> =
6666
: // Otherwise, try to infer the type and enforce the schema
6767
NonNullable<Value> extends Array<infer U> | ReadonlyArray<infer U>
6868
? SchemaArray<U, Value>
69+
: NonNullable<Value> extends Date
70+
? SchemaChildValue<Value>
6971
: NonNullable<Value> extends object
7072
? SchemaObject<Value>
7173
: SchemaChildValue<Value>);

0 commit comments

Comments
 (0)