• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rsuite / schema-typed / 8657282119

12 Apr 2024 05:09AM UTC coverage: 95.227%. Remained the same
8657282119

Pull #80

github

web-flow
Merge 1ad0891ec into 9b4a8f368
Pull Request #80: refactor: replace `lodash.set` with `set-value`

332 of 369 branches covered (89.97%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 1 file covered. (100.0%)

466 of 469 relevant lines covered (99.36%)

198.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/StringType.ts
1
import { MixedType } from './MixedType';
3✔
2
import { ErrorMessageType } from './types';
3
import { StringTypeLocale } from './locales';
4

5
export class StringType<DataType = any, E = ErrorMessageType> extends MixedType<
3✔
6
  string,
7
  DataType,
8
  E,
9
  StringTypeLocale
10
> {
11
  constructor(errorMessage?: E | string) {
12
    super('string');
468✔
13
    super.pushRule({
468✔
14
      onValid: v => typeof v === 'string',
654✔
15
      errorMessage: errorMessage || this.locale.type
927✔
16
    });
17
  }
18

19
  containsLetter(errorMessage: E | string = this.locale.containsLetter) {
3✔
20
    super.pushRule({
3✔
21
      onValid: v => /[a-zA-Z]/.test(v),
12✔
22
      errorMessage
23
    });
24
    return this;
3✔
25
  }
26

27
  containsUppercaseLetter(errorMessage: E | string = this.locale.containsUppercaseLetter) {
3✔
28
    super.pushRule({
3✔
29
      onValid: v => /[A-Z]/.test(v),
12✔
30
      errorMessage
31
    });
32
    return this;
3✔
33
  }
34

35
  containsLowercaseLetter(errorMessage: E | string = this.locale.containsLowercaseLetter) {
3✔
36
    super.pushRule({
3✔
37
      onValid: v => /[a-z]/.test(v),
12✔
38
      errorMessage
39
    });
40
    return this;
3✔
41
  }
42

43
  containsLetterOnly(errorMessage: E | string = this.locale.containsLetterOnly) {
3✔
44
    super.pushRule({
3✔
45
      onValid: v => /^[a-zA-Z]+$/.test(v),
15✔
46
      errorMessage
47
    });
48
    return this;
3✔
49
  }
50

51
  containsNumber(errorMessage: E | string = this.locale.containsNumber) {
3✔
52
    super.pushRule({
3✔
53
      onValid: v => /[0-9]/.test(v),
12✔
54
      errorMessage
55
    });
56
    return this;
3✔
57
  }
58

59
  isOneOf(values: string[], errorMessage: E | string = this.locale.isOneOf) {
6✔
60
    super.pushRule({
9✔
61
      onValid: v => !!~values.indexOf(v),
18✔
62
      errorMessage,
63
      params: { values }
64
    });
65
    return this;
9✔
66
  }
67

68
  isEmail(errorMessage: E | string = this.locale.isEmail) {
60✔
69
    // http://emailregex.com/
70
    const regexp =
71
      /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
120✔
72
    super.pushRule({
120✔
73
      onValid: v => regexp.test(v),
129✔
74
      errorMessage
75
    });
76
    return this;
120✔
77
  }
78

79
  isURL(
80
    errorMessage: E | string = this.locale.isURL,
3✔
81
    options?: {
82
      allowMailto?: boolean;
83
    }
84
  ) {
85
    const regexp = new RegExp(
21✔
86
      options?.allowMailto ?? false
147✔
87
        ? '^(?:mailto:|(?:(?:http|https|ftp)://|//))(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$'
88
        : '^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
89
      'i'
90
    );
91
    super.pushRule({
21✔
92
      onValid: v => regexp.test(v),
27✔
93
      errorMessage
94
    });
95
    return this;
21✔
96
  }
97
  isHex(errorMessage: E | string = this.locale.isHex) {
3✔
98
    const regexp = /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i;
3✔
99
    super.pushRule({
3✔
100
      onValid: v => regexp.test(v),
21✔
101
      errorMessage
102
    });
103
    return this;
3✔
104
  }
105
  pattern(regexp: RegExp, errorMessage: E | string = this.locale.pattern) {
3✔
106
    super.pushRule({
3✔
107
      onValid: v => regexp.test(v),
12✔
108
      errorMessage,
109
      params: { regexp }
110
    });
111
    return this;
3✔
112
  }
113

114
  rangeLength(
115
    minLength: number,
116
    maxLength: number,
117
    errorMessage: E | string = this.locale.rangeLength
3✔
118
  ) {
119
    super.pushRule({
3✔
120
      onValid: value => value.length >= minLength && value.length <= maxLength,
12✔
121
      errorMessage,
122
      params: { minLength, maxLength }
123
    });
124
    return this;
3✔
125
  }
126

127
  minLength(minLength: number, errorMessage: E | string = this.locale.minLength) {
9✔
128
    super.pushRule({
15✔
129
      onValid: value => Array.from(value).length >= minLength,
33✔
130
      errorMessage,
131
      params: { minLength }
132
    });
133
    return this;
15✔
134
  }
135

136
  maxLength(maxLength: number, errorMessage: E | string = this.locale.maxLength) {
6✔
137
    super.pushRule({
15✔
138
      onValid: value => Array.from(value).length <= maxLength,
45✔
139
      errorMessage,
140
      params: { maxLength }
141
    });
142
    return this;
15✔
143
  }
144
}
145

146
export default function getStringType<DataType = any, E = string>(errorMessage?: E) {
3✔
147
  return new StringType<DataType, E>(errorMessage);
468✔
148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc