fix(striker-ui): simplify common input test cases

main
Tsu-ba-me 2 years ago
parent 4488477f3b
commit 4b80587f57
  1. 32
      striker-ui/lib/test_input/buildDomainTestBatch.tsx
  2. 26
      striker-ui/lib/test_input/buildIPAddressTestBatch.tsx
  3. 34
      striker-ui/lib/test_input/buildPeacefulStringTestBatch.tsx
  4. 6
      striker-ui/lib/test_input/index.ts
  5. 30
      striker-ui/types/TestInputFunction.d.ts

@ -0,0 +1,32 @@
import { REP_DOMAIN } from '../consts/REG_EXP_PATTERNS';
import testNotBlank from './testNotBlank';
import { InlineMonoText } from '../../components/Text';
const buildDomainTestBatch: BuildInputTestBatchFunction = (
inputName,
onSuccess,
{ compare: dCompare, getValue } = {},
onDomainTestFailure,
) => ({
defaults: { compare: dCompare, getValue, onSuccess },
tests: [
{
onFailure: (...args) => {
onDomainTestFailure(
<>
{inputName} can only contain lowercase alphanumeric, hyphen (
<InlineMonoText text="-" />
), and dot (<InlineMonoText text="." />) characters.
</>,
...args,
);
},
test: ({ compare, value }) =>
(compare[0] as boolean) || REP_DOMAIN.test(value as string),
},
{ test: testNotBlank },
],
});
export default buildDomainTestBatch;

@ -0,0 +1,26 @@
import { REP_IPV4 } from '../consts/REG_EXP_PATTERNS';
import testNotBlank from './testNotBlank';
const buildIPAddressTestBatch: BuildInputTestBatchFunction = (
inputName,
onSuccess,
{ getValue } = {},
onIPv4TestFailure,
) => ({
defaults: { getValue, onSuccess },
tests: [
{
onFailure: (...args) => {
onIPv4TestFailure(
`${inputName} should be a valid IPv4 address.`,
...args,
);
},
test: ({ value }) => REP_IPV4.test(value as string),
},
{ test: testNotBlank },
],
});
export default buildIPAddressTestBatch;

@ -0,0 +1,34 @@
import testNotBlank from './testNotBlank';
import { InlineMonoText } from '../../components/Text';
const buildPeacefulStringTestBatch: BuildInputTestBatchFunction = (
inputName,
onSuccess,
{ getValue } = {},
onTestPeacefulStringFailureAppend,
) => ({
defaults: { getValue, onSuccess },
tests: [
{
onFailure: (...args) => {
onTestPeacefulStringFailureAppend(
<>
{inputName} cannot contain single-quote (
<InlineMonoText text="'" />
), double-quote (<InlineMonoText text='"' />
), slash (<InlineMonoText text="/" />
), backslash (<InlineMonoText text="\" />
), angle brackets (<InlineMonoText text="<>" />
), curly brackets (<InlineMonoText text="{}" />
).
</>,
...args,
);
},
test: ({ value }) => !/['"/\\><}{]/g.test(value as string),
},
{ test: testNotBlank },
],
});
export default buildPeacefulStringTestBatch;

@ -1,3 +1,6 @@
import buildDomainTestBatch from './buildDomainTestBatch';
import buildIPAddressTestBatch from './buildIPAddressTestBatch';
import buildPeacefulStringTestBatch from './buildPeacefulStringTestBatch';
import createTestInputFunction from './createTestInputFunction';
import testInput from './testInput';
import testLength from './testLength';
@ -6,6 +9,9 @@ import testNotBlank from './testNotBlank';
import testRange from './testRange';
export {
buildDomainTestBatch,
buildIPAddressTestBatch,
buildPeacefulStringTestBatch,
createTestInputFunction,
testInput,
testLength,

@ -33,6 +33,11 @@ type InputTestFailureCallback = (
args: InputTestArgs & CallbackAppendArgs,
) => void;
type InputTestFailureAppendCallback = (
message: import('react').ReactNode,
...args: Parameters<InputTestFailureCallback>
) => void;
type InputTestSuccessCallback = (args: CallbackAppendArgs) => void;
type InputTest = {
@ -47,15 +52,24 @@ type InputTestInputs = {
type InputTestBatchFinishCallback = () => void;
type InputTestBatches = {
[id: string]: {
defaults?: InputTestArgs & {
onSuccess?: InputTestSuccessCallback;
};
onFinishBatch?: InputTestBatchFinishCallback;
optionalTests?: Array<InputTest>;
tests: Array<InputTest>;
type InputTestBatch = {
defaults?: InputTestArgs & {
onSuccess?: InputTestSuccessCallback;
};
onFinishBatch?: InputTestBatchFinishCallback;
optionalTests?: Array<InputTest>;
tests: Array<InputTest>;
};
type BuildInputTestBatchFunction = (
inputName: string,
onSuccess: InputTestSuccessCallback,
options?: InputTestBatch['defaults'],
...onFailureAppends: InputTestFailureAppendCallback[]
) => InputTestBatch;
type InputTestBatches = {
[id: string]: InputTestBatch;
};
type TestInputFunctionOptions = {

Loading…
Cancel
Save