MCPcopy Create free account
hub / github.com/SmartThingsCommunity/smartthings-cli / stringValidateFn

Function stringValidateFn

src/lib/validate-util.ts:36–55  ·  view source on GitHub ↗
(options: StringValidationOptions)

Source from the content-addressed store, hash-verified

34 * conditions.
35 */
36export const stringValidateFn = (options: StringValidationOptions): ValidateFunction<string> => {
37 if ('regex' in options) {
38 return (input: string): true | string => {
39 const regex = typeof options.regex === 'string' ? new RegExp(options.regex) : options.regex
40 return regex.test(input) || (options.errorMessage ?? `must match regex ${regex}`)
41 }
42 }
43 if ('minLength' in options && 'maxLength' in options && options.minLength > options.maxLength) {
44 throw Error('maxLength must be >= minLength')
45 }
46 return (input: string): true | string => {
47 if ('minLength' in options && input.length < options.minLength) {
48 return `must be at least ${options.minLength} characters`
49 }
50 if ('maxLength' in options && input.length > options.maxLength) {
51 return `must be no more than ${options.maxLength} characters`
52 }
53 return true
54 }
55}
56
57export type NumberValidateOptions = {
58 min?: number

Callers 6

create.tsFile · 0.85
promptAndAddAttributeFunction · 0.85
getInputFromUserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected