Skip to content

Commit

Permalink
fix: remove validation already enforced by types
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Oct 27, 2022
1 parent 573cb05 commit 9d16ee5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
12 changes: 5 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,18 @@ export function mapAliases<Schemas extends Flags>(
if (schema && typeof schema === 'object') {
const { alias } = schema;
if (typeof alias === 'string') {
const errorPrefix = `Flag alias ${stringify(alias)} for flag ${stringify(flagName)}`;

if (alias.length === 0) {
throw new Error(`Flag alias for ${stringify(flagName)} cannot be empty`);
throw new Error(`${errorPrefix} cannot be empty`);
}

if (alias.length > 1) {
throw new Error(`Flag alias for ${stringify(flagName)} must be a single character`);
throw new Error(`${errorPrefix} must be a single character`);
}

if (aliases.has(alias)) {
throw new Error(`Flag alias ${stringify(alias)} for ${stringify(flagName)} is already used`);
throw new Error(`${errorPrefix} is already used`);
}

aliases.set(alias, {
Expand Down Expand Up @@ -204,10 +206,6 @@ export const getFlagType = (
flagName: string,
flagSchema: FlagTypeOrSchema,
): TypeFunction => {
if (!flagSchema) {
throw new Error(`Missing type on flag ${stringify(flagName)}`);
}

if (typeof flagSchema === 'function') {
return flagSchema;
}
Expand Down
12 changes: 0 additions & 12 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,6 @@ describe('Error handling', ({ describe, test }) => {
}).toThrow(/* 'Flag collision: Alias "a" is already used' */);
});
});

test('Missing required type', () => {
expect(() => {
typeFlag({
// @ts-expect-error missing
missingType: {
alias: 't',
},

}, ['--missing-type']);
}).toThrow('Missing type on flag "missingType"');
});
});

describe('Types', ({ test }) => {
Expand Down

0 comments on commit 9d16ee5

Please # to comment.