Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EBT] Fix schema support to Date #132051

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions packages/analytics/client/src/schema/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,66 @@ describe('schema types', () => {
});
});

describe('Date value', () => {
test('it should allow the correct type and enforce the _meta.description', () => {
let valueType: SchemaValue<Date> = {
type: 'date',
_meta: {
description: 'Some description',
},
};

valueType = {
type: 'keyword',
_meta: {
description: 'Some description',
optional: false,
},
};

valueType = {
// @ts-expect-error because the type does not match
type: 'long',
_meta: {
description: 'Some description',
optional: false,
},
};

valueType = {
type: 'keyword',
_meta: {
description: 'Some description',
// @ts-expect-error optional can't be true when the types don't set the value as optional
optional: true,
},
};

// @ts-expect-error because it's missing the _meta.description
valueType = { type: 'date' };
expect(valueType).not.toBeUndefined(); // <-- Only to stop the var-not-used complain
});
test('it should enforce `_meta.optional: true`', () => {
let valueType: SchemaValue<Date | undefined> = {
type: 'date',
_meta: {
description: 'Some description',
optional: true,
},
};

valueType = {
type: 'date',
_meta: {
description: 'Some description',
// @ts-expect-error because optional can't be false when the value can be undefined
optional: false,
},
};
expect(valueType).not.toBeUndefined(); // <-- Only to stop the var-not-used complain
});
});

describe('Object value', () => {
test('it should allow "pass_through" and enforce the _meta.description', () => {
let valueType: SchemaValue<{ a_value: string }> = {
Expand Down
4 changes: 3 additions & 1 deletion packages/analytics/client/src/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type AllowedSchemaTypes =
/**
* Helper to ensure the declared types match the schema types
*/
export type PossibleSchemaTypes<Value> = Value extends string
export type PossibleSchemaTypes<Value> = Value extends string | Date
? AllowedSchemaStringTypes
: Value extends number
? AllowedSchemaNumberTypes
Expand Down Expand Up @@ -66,6 +66,8 @@ export type SchemaValue<Value> =
: // Otherwise, try to infer the type and enforce the schema
NonNullable<Value> extends Array<infer U> | ReadonlyArray<infer U>
? SchemaArray<U, Value>
: NonNullable<Value> extends Date
? SchemaChildValue<Value>
: NonNullable<Value> extends object
? SchemaObject<Value>
: SchemaChildValue<Value>);
Expand Down