Skip to content

Commit

Permalink
fix(store): improve error for forbidden characters in createActionGro…
Browse files Browse the repository at this point in the history
…up (#3496)
  • Loading branch information
markostanimirovic committed Jul 21, 2022
1 parent bdb8634 commit 398fbed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
6 changes: 3 additions & 3 deletions modules/store/spec/types/action_group_creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('createActionGroup', () => {
'${char}Load Books Success': emptyProps(),
},
});
`).toFail(/event name cannot contain/);
`).toFail(/event name cannot contain the following characters:/);
});

it(`should fail when event name contains ${char} in the middle`, () => {
Expand All @@ -175,7 +175,7 @@ describe('createActionGroup', () => {
'Load Books ${char} Success': emptyProps(),
},
});
`).toFail(/event name cannot contain/);
`).toFail(/event name cannot contain the following characters:/);
});

it(`should fail when event name contains ${char} in the end`, () => {
Expand All @@ -186,7 +186,7 @@ describe('createActionGroup', () => {
'Load Books Success${char}': emptyProps(),
},
});
`).toFail(/event name cannot contain/);
`).toFail(/event name cannot contain the following characters:/);
});
});
});
Expand Down
38 changes: 10 additions & 28 deletions modules/store/src/action_group_creator_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,21 @@ type TitleCase<Str extends string> = Str extends `${infer First} ${infer Rest}`
? `${Capitalize<First>} ${TitleCase<Rest>}`
: Capitalize<Str>;

type ForbiddenCharacters =
| '/'
| '\\'
| '|'
| '<'
| '>'
| '['
| ']'
| '{'
| '}'
| '('
| ')'
| '.'
| ','
| '!'
| '?'
| '#'
| '%'
| '^'
| '&'
| '*'
| '+'
| '-'
| '~'
| "'"
| '"'
| '`';
type ForbiddenCharactersStr =
'/ \\ | < > [ ] { } ( ) . , ! ? # % ^ & * + - ~ \' " `';

type ForbiddenCharacters<Str extends string = ForbiddenCharactersStr> =
Str extends `${infer First} ${infer Rest}`
? First | ForbiddenCharacters<Rest>
: Str extends ''
? never
: Str;

type ForbiddenCharactersCheck<
Str extends string,
Name extends string
> = Str extends `${infer _}${ForbiddenCharacters}${infer _}`
? `${Name} cannot contain ${ForbiddenCharacters}`
? `${Name} cannot contain the following characters: ${ForbiddenCharactersStr}`
: unknown;

type EmptyStringCheck<
Expand Down

0 comments on commit 398fbed

Please sign in to comment.