本文整理汇总了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)
})
})
})
示例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)
})
})
})
})
示例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'))
})
示例4: it
it('when both are valid strings', () => {
const errors = validator.validateSync(new TimelineRow('Date', 'description'))
expect(errors.length).to.equal(0)
})
示例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)
})
示例6: it
it('should return no error', () => {
const errors = validator.validateSync(new IncomeExpenseSource(100, IncomeExpenseSchedule.MONTH))
expect(errors.length).to.equal(0)
})
示例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)
})