當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript class-validator.Validator類代碼示例

本文整理匯總了TypeScript中@hmcts/class-validator.Validator的典型用法代碼示例。如果您正苦於以下問題:TypeScript Validator類的具體用法?TypeScript Validator怎麽用?TypeScript Validator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Validator類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: describe

  describe('validation', () => {

    const validator: Validator = new Validator()

    context('should accept', () => {
      it('when both undefined', () => {
        const errors = validator.validateSync(new TimelineRow(undefined, undefined))

        expect(errors.length).to.equal(0)
      })

      it('when both are valid strings', () => {
        const errors = validator.validateSync(new TimelineRow('Date', 'description'))

        expect(errors.length).to.equal(0)
      })
    })

    context('should reject', () => {
      it('when date given, but no description', () => {
        const errors = validator.validateSync(new TimelineRow('May', ''))

        expect(errors.length).to.equal(1)
        expectValidationError(errors, ValidationErrors.DESCRIPTION_REQUIRED)
      })

      it('when description given, but no date', () => {
        const errors = validator.validateSync(new TimelineRow('', 'Let me tell you what happened'))

        expect(errors.length).to.equal(1)
        expectValidationError(errors, GlobalValidationErrors.DATE_REQUIRED)
      })

      it('when both args are empty strings', () => {
        const errors = validator.validateSync(new TimelineRow('', ''))

        expect(errors.length).to.equal(2)
        expectValidationError(errors, GlobalValidationErrors.DATE_REQUIRED)
        expectValidationError(errors, ValidationErrors.DESCRIPTION_REQUIRED)
      })

      it('when date is too long', () => {
        const errors = validator.validateSync(
          new TimelineRow(generateString(ValidationConstraints.DATE_MAX_LENGTH + 1), 'description')
        )

        expect(errors.length).to.equal(1)
        expectValidationError(
          errors, ValidationErrors.DATE_TOO_LONG.replace('$constraint1', ValidationConstraints.DATE_MAX_LENGTH.toString())
        )
      })

      it('when description is too long', () => {
        const errors = validator.validateSync(
          new TimelineRow('date', generateString(DefaultValidationConstraints.FREE_TEXT_MAX_LENGTH + 1))
        )

        expect(errors.length).to.equal(1)
        expectValidationError(errors, GlobalValidationErrors.TEXT_TOO_LONG)
      })
    })
  })
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:62,代碼來源:timelineRow.ts

示例2: describe

describe('HowMuchHaveYouPaid', () => {
  const validator: Validator = new Validator()

  context('should not be valid when', () => {
    context('amount is', () => {
      it('eq 0', () => {
        const errors = validator.validateSync(new HowMuchHaveYouPaid(0, validLocalDate, validText))
        expect(errors.length).to.equal(1)
        expectValidationError(errors, ValidationErrors.AMOUNT_NOT_VALID)
      })

      it('less than 0', () => {
        const errors = validator.validateSync(new HowMuchHaveYouPaid(-10, validLocalDate, validText))
        expect(errors.length).to.equal(1)
        expectValidationError(errors, ValidationErrors.AMOUNT_NOT_VALID)
      })
    })

    context('date is', () => {
      it('in the future', () => {
        const dateInThePast = LocalDate.fromObject({ day: 10, month: 10, year: 2200 })
        const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, dateInThePast, validText))
        expect(errors.length).to.equal(1)
        expectValidationError(errors, ValidationErrors.VALID_PAST_DATE)
      })

      it('invalid', () => {
        const dateInThePast = LocalDate.fromObject({ day: 33, month: 13, year: 1990 })
        const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, dateInThePast, validText))
        expect(errors.length).to.equal(1)
        expectValidationError(errors, LocalDateValidationErrors.DAY_NOT_VALID)
      })

      it('undefined', () => {
        const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, undefined, validText))
        expect(errors.length).to.equal(1)
        expectValidationError(errors, DefaultValidationErrors.DATE_REQUIRED)
      })
    })

    context('text is', () => {
      it('empty', () => {
        const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, validLocalDate, ''))
        expect(errors.length).to.equal(1)
        expectValidationError(errors, ValidationErrors.EXPLANATION_REQUIRED)
      })

      it('undefined', () => {
        const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, validLocalDate, undefined))
        expect(errors.length).to.equal(1)
        expectValidationError(errors, ValidationErrors.EXPLANATION_REQUIRED)
      })

      it('too long', () => {
        const errors = validator.validateSync(new HowMuchHaveYouPaid(
          validAmount, validLocalDate, generateString(ValidationConstraints.FREE_TEXT_MAX_LENGTH + 1)
        ))
        expect(errors.length).to.equal(1)
        expectValidationError(errors, DefaultValidationErrors.TEXT_TOO_LONG)
      })
    })
  })
})
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:63,代碼來源:howMuchHaveYouPaid.ts

示例3: it

      it('should return an error when `schedule` is invalid', () => {
        const errors = validator.validateSync(new ExpenseSource('Source name', 100, new IncomeExpenseSchedule('UNKNOWN', 'Unknown')))

        expect(errors.length).to.equal(1)
        expectValidationError(errors, ValidationErrors.SCHEDULE_SELECT_AN_OPTION('Source name'))
      })
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:6,代碼來源:expenseSource.ts

示例4: it

      it('when both are valid strings', () => {
        const errors = validator.validateSync(new TimelineRow('Date', 'description'))

        expect(errors.length).to.equal(0)
      })
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:5,代碼來源:timelineRow.ts

示例5: it

 it('in the future', () => {
   const dateInThePast = LocalDate.fromObject({ day: 10, month: 10, year: 2200 })
   const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, dateInThePast, validText))
   expect(errors.length).to.equal(1)
   expectValidationError(errors, ValidationErrors.VALID_PAST_DATE)
 })
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:6,代碼來源:howMuchHaveYouPaid.ts

示例6: it

 it('should return no error', () => {
   const errors = validator.validateSync(new IncomeExpenseSource(100, IncomeExpenseSchedule.MONTH))
   expect(errors.length).to.equal(0)
 })
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:4,代碼來源:incomeExpenseSource.ts

示例7: it

      it('when declared = true and one valid row and many many empty ones given', () => {
        const o: DebtRow = DebtRow.empty()
        const errors = validator.validateSync(new Debts(true, [o, o, o, o, new DebtRow('card', 1, 1), o, o, o, o]))

        expect(errors.length).to.equal(0)
      })
開發者ID:hmcts,項目名稱:cmc-citizen-frontend,代碼行數:6,代碼來源:debts.ts


注:本文中的@hmcts/class-validator.Validator類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。