|
| 1 | +/* eslint-disable import/no-extraneous-dependencies */ |
| 2 | +import { expect } from "chai"; |
| 3 | +import j from "joi"; |
| 4 | +import FormInterface from "../../../../src/models/FormInterface"; |
| 5 | +import OptionsModel from "../../../../src/models/OptionsModel"; |
| 6 | +import { Form } from "../../../../src"; |
| 7 | +import joi from "../../../../src/validators/JOI"; |
| 8 | +import { ValidationPlugins } from "../../../../src/models/ValidatorInterface"; |
| 9 | + |
| 10 | + |
| 11 | +const fields = [ |
| 12 | + "user.username", |
| 13 | + "user.email", |
| 14 | + "user.password", |
| 15 | + "user.passwordConfirm", |
| 16 | +]; |
| 17 | + |
| 18 | +const values = { |
| 19 | + user: { |
| 20 | + username: 'a', |
| 21 | + email: 'notAValidEmail@', |
| 22 | + password: 'x', |
| 23 | + passwordConfirm: 'mysecretpassword', |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const schema = j.object({ |
| 28 | + user: j.object({ |
| 29 | + username: j.string().min(3).required().label('Username'), |
| 30 | + email: j.string().email().required(), |
| 31 | + password: j.string().min(6).max(25).required(), |
| 32 | + passwordConfirm: j.string().min(6).max(25).valid(j.ref('password')).required().messages({ |
| 33 | + 'any.only': 'Passwords do not match', |
| 34 | + 'any.required': 'Password confirmation is required', |
| 35 | + }), |
| 36 | + }).required() |
| 37 | +}); |
| 38 | + |
| 39 | +const plugins: ValidationPlugins = { |
| 40 | + joi: joi({ |
| 41 | + package: j, |
| 42 | + schema, |
| 43 | + }), |
| 44 | +}; |
| 45 | + |
| 46 | +const options: OptionsModel = { |
| 47 | + validateOnInit: true, |
| 48 | + showErrorsOnInit: true, |
| 49 | +}; |
| 50 | + |
| 51 | +export default new Form({ |
| 52 | + fields, |
| 53 | + values, |
| 54 | +}, { |
| 55 | + plugins, |
| 56 | + options, |
| 57 | + name: "Nested-Z3", |
| 58 | + hooks: { |
| 59 | + onInit(form: FormInterface) { |
| 60 | + describe("Check joi validation flag", () => { |
| 61 | + it('user.username hasError should be true', () => expect(form.$('user.username').hasError).to.be.true); |
| 62 | + it('user.email hasError should be true', () => expect(form.$('user.email').hasError).to.be.true); |
| 63 | + it('user.password hasError should be true', () => expect(form.$('user.password').hasError).to.be.true); |
| 64 | + it('user.passwordConfirm hasError should be true', () => expect(form.$('user.passwordConfirm').hasError).to.be.true); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("Check joi validation errors", () => { |
| 68 | + it('user.username error should equal joi error', () => expect(form.$('user.username').error).to.be.equal('"Username" length must be at least 3 characters long')); |
| 69 | + it('user.email error should equal joi error', () => expect(form.$('user.email').error).to.be.equal('"user.email" must be a valid email')); |
| 70 | + it('user.password error should equal joi error', () => expect(form.$('user.password').error).to.be.equal('"user.password" length must be at least 6 characters long')); |
| 71 | + it('user.passwordConfirm error should equal joi error', () => expect(form.$('user.passwordConfirm').error).to.be.equal('Passwords do not match')); |
| 72 | + }); |
| 73 | + |
| 74 | + } |
| 75 | + } |
| 76 | +}); |
0 commit comments