-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation): add slack channel name validation (#488)
- Loading branch information
1 parent
1079e3b
commit a74e820
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createRule } from '../Rule' | ||
import { type SlackChannelNameOptions, slackChannelName as baseSlackChannelName } from '../validators' | ||
|
||
export const message = { | ||
en: 'The slack channel name is invalid.', | ||
ja: 'Slackチャンネル名の形式が正しくありません。' | ||
} | ||
|
||
export function slackChannelName(options?: SlackChannelNameOptions, msg?: string) { | ||
return createRule({ | ||
message: ({ lang }) => msg ?? message[lang], | ||
optional: true, | ||
validation: (value) => baseSlackChannelName(value, options) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { isString } from '../../support/Utils' | ||
|
||
export interface SlackChannelNameOptions { | ||
offset?: number | ||
} | ||
|
||
export function slackChannelName(value: unknown, options: SlackChannelNameOptions = {}): boolean { | ||
if (!isString(value)) { | ||
return false | ||
} | ||
|
||
const { offset = 0 } = options | ||
const maxLength = /* Slack channel name max length */ 80 - offset | ||
|
||
return new RegExp(`^[\\p{L}\\p{M}\\p{N}_-]{1,${maxLength}}$`, 'u').test(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { slackChannelName } from 'sefirot/validation/rules' | ||
|
||
describe('validation/rules/slackChannelName', () => { | ||
test('it validates whether the value is valid', () => { | ||
const rule = slackChannelName() | ||
|
||
expect(rule.$validator('1'.repeat(80), null, null)).toBe(true) | ||
expect(rule.$validator(undefined, null, null)).toBe(true) | ||
expect(rule.$validator(null, null, null)).toBe(true) | ||
expect(rule.$validator('', null, null)).toBe(true) | ||
expect(rule.$validator([], null, null)).toBe(true) | ||
|
||
expect(rule.$validator('1'.repeat(81), null, null)).toBe(false) | ||
expect(rule.$validator(' ', null, null)).toBe(false) | ||
expect(rule.$validator(',', null, null)).toBe(false) | ||
expect(rule.$validator('.', null, null)).toBe(false) | ||
expect(rule.$validator('!', null, null)).toBe(false) | ||
expect(rule.$validator('@', null, null)).toBe(false) | ||
expect(rule.$validator('#', null, null)).toBe(false) | ||
expect(rule.$validator('$', null, null)).toBe(false) | ||
expect(rule.$validator('%', null, null)).toBe(false) | ||
expect(rule.$validator('^', null, null)).toBe(false) | ||
expect(rule.$validator('&', null, null)).toBe(false) | ||
expect(rule.$validator('*', null, null)).toBe(false) | ||
expect(rule.$validator('?', null, null)).toBe(false) | ||
expect(rule.$validator('(', null, null)).toBe(false) | ||
expect(rule.$validator(')', null, null)).toBe(false) | ||
expect(rule.$validator('{', null, null)).toBe(false) | ||
expect(rule.$validator('}', null, null)).toBe(false) | ||
expect(rule.$validator('<', null, null)).toBe(false) | ||
expect(rule.$validator('>', null, null)).toBe(false) | ||
expect(rule.$validator('=', null, null)).toBe(false) | ||
expect(rule.$validator('+', null, null)).toBe(false) | ||
expect(rule.$validator('|', null, null)).toBe(false) | ||
expect(rule.$validator('・', null, null)).toBe(false) | ||
// eslint-disable-next-line @typescript-eslint/quotes | ||
expect(rule.$validator("'", null, null)).toBe(false) | ||
expect(rule.$validator('`', null, null)).toBe(false) | ||
expect(rule.$validator('~', null, null)).toBe(false) | ||
expect(rule.$validator('/', null, null)).toBe(false) | ||
expect(rule.$validator('\\', null, null)).toBe(false) | ||
expect(rule.$validator('[', null, null)).toBe(false) | ||
expect(rule.$validator(']', null, null)).toBe(false) | ||
expect(rule.$validator('"', null, null)).toBe(false) | ||
expect(rule.$validator(true, null, null)).toBe(false) | ||
expect(rule.$validator(false, null, null)).toBe(false) | ||
expect(rule.$validator(1, null, null)).toBe(false) | ||
expect(rule.$validator({}, null, null)).toBe(false) | ||
}) | ||
|
||
test('it validates whether the value is valid with offset', () => { | ||
const rule = slackChannelName({ | ||
offset: 10 | ||
}) | ||
|
||
expect(rule.$validator('1'.repeat(70), null, null)).toBe(true) | ||
expect(rule.$validator(undefined, null, null)).toBe(true) | ||
expect(rule.$validator(null, null, null)).toBe(true) | ||
expect(rule.$validator('', null, null)).toBe(true) | ||
expect(rule.$validator([], null, null)).toBe(true) | ||
|
||
expect(rule.$validator('1'.repeat(71), null, null)).toBe(false) | ||
expect(rule.$validator(true, null, null)).toBe(false) | ||
expect(rule.$validator(false, null, null)).toBe(false) | ||
expect(rule.$validator(1, null, null)).toBe(false) | ||
expect(rule.$validator({}, null, null)).toBe(false) | ||
}) | ||
|
||
test('default error message', () => { | ||
const rule = slackChannelName() | ||
expect(rule.$message({ $params: {} })).toBe('The slack channel name is invalid.') | ||
}) | ||
|
||
test('it can set custom error message', () => { | ||
const rule = slackChannelName({}, 'Custom message.') | ||
expect(rule.$message({ $params: {} })).toBe('Custom message.') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { slackChannelName } from 'sefirot/validation/validators' | ||
|
||
describe('validation/validators/slackChannelName', () => { | ||
test('it validates whether the value is valid', () => { | ||
expect(slackChannelName('1'.repeat(80))).toBe(true) | ||
|
||
expect(slackChannelName('')).toBe(false) | ||
expect(slackChannelName('1'.repeat(81))).toBe(false) | ||
expect(slackChannelName(' ')).toBe(false) | ||
expect(slackChannelName(',')).toBe(false) | ||
expect(slackChannelName('.')).toBe(false) | ||
expect(slackChannelName('!')).toBe(false) | ||
expect(slackChannelName('@')).toBe(false) | ||
expect(slackChannelName('#')).toBe(false) | ||
expect(slackChannelName('$')).toBe(false) | ||
expect(slackChannelName('%')).toBe(false) | ||
expect(slackChannelName('^')).toBe(false) | ||
expect(slackChannelName('&')).toBe(false) | ||
expect(slackChannelName('*')).toBe(false) | ||
expect(slackChannelName('?')).toBe(false) | ||
expect(slackChannelName('(')).toBe(false) | ||
expect(slackChannelName(')')).toBe(false) | ||
expect(slackChannelName('{')).toBe(false) | ||
expect(slackChannelName('}')).toBe(false) | ||
expect(slackChannelName('<')).toBe(false) | ||
expect(slackChannelName('>')).toBe(false) | ||
expect(slackChannelName('=')).toBe(false) | ||
expect(slackChannelName('+')).toBe(false) | ||
expect(slackChannelName('|')).toBe(false) | ||
expect(slackChannelName('・')).toBe(false) | ||
// eslint-disable-next-line @typescript-eslint/quotes | ||
expect(slackChannelName("'")).toBe(false) | ||
expect(slackChannelName('`')).toBe(false) | ||
expect(slackChannelName('~')).toBe(false) | ||
expect(slackChannelName('/')).toBe(false) | ||
expect(slackChannelName('\\')).toBe(false) | ||
expect(slackChannelName('[')).toBe(false) | ||
expect(slackChannelName(']')).toBe(false) | ||
expect(slackChannelName('"')).toBe(false) | ||
expect(slackChannelName(undefined)).toBe(false) | ||
expect(slackChannelName(null)).toBe(false) | ||
expect(slackChannelName(true)).toBe(false) | ||
expect(slackChannelName(false)).toBe(false) | ||
expect(slackChannelName(1)).toBe(false) | ||
expect(slackChannelName([])).toBe(false) | ||
expect(slackChannelName({})).toBe(false) | ||
}) | ||
|
||
test('it validates whether the value is valid with offset', () => { | ||
const options = { | ||
offset: 10 | ||
} | ||
|
||
expect(slackChannelName('1'.repeat(70), options)).toBe(true) | ||
|
||
expect(slackChannelName('', options)).toBe(false) | ||
expect(slackChannelName('1'.repeat(71), options)).toBe(false) | ||
expect(slackChannelName(undefined, options)).toBe(false) | ||
expect(slackChannelName(null, options)).toBe(false) | ||
expect(slackChannelName(true, options)).toBe(false) | ||
expect(slackChannelName(false, options)).toBe(false) | ||
expect(slackChannelName(1, options)).toBe(false) | ||
expect(slackChannelName([], options)).toBe(false) | ||
expect(slackChannelName({}, options)).toBe(false) | ||
}) | ||
}) |