All files / modules/10-common/utils Validation.tsx

97.06% Statements 33/34
90.2% Branches 46/51
91.67% Functions 11/12
96.88% Lines 31/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102              412x 412x 412x           412x       1229x           412x 1168x 1168x     412x       1231x             412x       1110x 1669x         412x       1049x 1049x     412x 18x   18x 1x             2x   2x 2x       17x           412x 14x 14x           412x       412x 25x 25x        
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import * as Yup from 'yup'
import { useStrings, UseStringsReturn } from 'framework/strings'
import { illegalIdentifiers, regexEmail, regexIdentifier, regexName } from '@common/utils/StringUtils'
interface EmailProps {
  allowMultiple?: boolean
  emailSeparator?: string
}
 
export function NameSchemaWithoutHook(
  getString: UseStringsReturn['getString'],
  config?: { requiredErrorMsg?: string }
): Yup.Schema<string> {
  return Yup.string()
    .trim()
    .required(config?.requiredErrorMsg ? config?.requiredErrorMsg : getString('common.validation.nameIsRequired'))
    .matches(regexName, getString('common.validation.namePatternIsNotValid'))
}
 
export function NameSchema(config?: { requiredErrorMsg?: string }): Yup.Schema<string> {
  const { getString } = useStrings()
  return NameSchemaWithoutHook(getString, config)
}
 
export function IdentifierSchemaWithOutName(
  getString: UseStringsReturn['getString'],
  config?: { requiredErrorMsg?: string; regexErrorMsg?: string }
): Yup.Schema<string | undefined> {
  return Yup.string()
    .trim()
    .matches(regexIdentifier, config?.regexErrorMsg ? config?.regexErrorMsg : getString('validation.validIdRegex'))
    .required(config?.requiredErrorMsg ? config?.requiredErrorMsg : getString('validation.identifierRequired'))
    .notOneOf(illegalIdentifiers)
}
 
export function IdentifierSchemaWithoutHook(
  getString: UseStringsReturn['getString'],
  config?: { requiredErrorMsg?: string; regexErrorMsg?: string }
): Yup.Schema<string | undefined> {
  return Yup.string().when('name', {
    is: val => val?.length,
    then: IdentifierSchemaWithOutName(getString, config)
  })
}
 
export function IdentifierSchema(config?: {
  requiredErrorMsg?: string
  regexErrorMsg?: string
}): Yup.Schema<string | undefined> {
  const { getString } = useStrings()
  return IdentifierSchemaWithoutHook(getString, config)
}
 
export function EmailSchema(emailProps: EmailProps = {}): Yup.Schema<string> {
  const { getString } = useStrings()
 
  if (emailProps.allowMultiple)
    return Yup.string()
      .trim()
      .required(getString('common.validation.email.required'))
      .test(
        'email',
        getString('common.validation.email.format'),
        value =>
          value &&
          value.split(emailProps.emailSeparator).every((emailString: string) => {
            const emailStringTrim = emailString.trim()
            return emailStringTrim ? Yup.string().email().isValidSync(emailStringTrim) : false
          })
      )
 
  return Yup.string()
    .trim()
    .required(getString('common.validation.email.required'))
    .email(getString('common.validation.email.format'))
}
 
export function URLValidationSchema(): Yup.Schema<string | undefined> {
  const { getString } = useStrings()
  return Yup.string()
    .trim()
    .required(getString('common.validation.urlIsRequired'))
    .url(getString('validation.urlIsNotValid'))
}
 
export const isEmail = (email: string): boolean => {
  return regexEmail.test(String(email).toLowerCase())
}
 
export const ConnectorRefSchema = (config?: { requiredErrorMsg?: string }): Yup.MixedSchema => {
  const { getString } = useStrings()
  return Yup.mixed().required(
    config?.requiredErrorMsg ? config?.requiredErrorMsg : getString('pipelineSteps.build.create.connectorRequiredError')
  )
}