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

feat: Support readonly options for Types.string and Types.number #956

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 17 additions & 5 deletions src/__tests__/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,23 @@ describe('types', () => {
});
});

test('options', () => {
types.number({
test('options, range and modulo', () => {
const options = {
maximum: 9,
minimum: 2,
multipleOf: 2,
});
} as const;
types.number(options);
Comment on lines +170 to +175
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not to be defined as const. Please keep this options object inline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct that this test case doesn't exercise any new readonly functionality. I'll revert this portion of the change.

In the subsequent test, the separate declaration is necessary.

image


// @ts-expect-error invalid option
types.number({ maxLength: 1 });
});

test('options, enum', () => {
const options = {
enum: [1, 2, 3],
} as const;
types.number(options);

// @ts-expect-error invalid option
types.number({ maxLength: 1 });
Expand Down Expand Up @@ -327,11 +338,12 @@ describe('types', () => {
});

test('options', () => {
types.string({
const options = {
enum: ['foo', 'bar'],
maxLength: 9,
minLength: 1,
});
} as const;
types.string(options);

// @ts-expect-error invalid option
types.string({ maximum: 1 });
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface ArrayOptions extends GenericOptions {
}

interface NumberOptions extends GenericOptions {
enum?: number[];
enum?: readonly number[];
exclusiveMaximum?: boolean;
exclusiveMinimum?: boolean;
maximum?: number;
Expand All @@ -35,7 +35,7 @@ interface ObjectOptions extends GenericOptions {
}

interface StringOptions extends GenericOptions {
enum?: string[];
enum?: readonly string[];
maxLength?: number;
minLength?: number;
pattern?: string;
Expand Down
Loading