HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: /var/www/javago_test/node_modules/node-input-validator/test/rules/sometimes.js
const assert = require('assert');

const { Validator } = require('../../lib/index');

describe('sometimes', () => {
  it('should fail', async () => {
    const v = new Validator(
      { password: '', confirm_password: 'password' },
      { password: 'sometimes', confirm_password: 'sometimes|alpha' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
  });

  it('should pass', async () => {
    const v = new Validator(
      { password: '000000', confirm_password: '000000' },
      { password: 'sometimes', confirm_password: 'sometimes|same:password' },
    );

    const matched = await v.check();

    assert.equal(matched, true);
  });

  it('should pass, attribute absent', async () => {
    const v = new Validator(
      {},
      { password: 'sometimes', confirm_password: 'sometimes|same:password' },
    );

    const matched = await v.check();

    assert.equal(matched, true);
  });

  it('message should exist', async () => {
    const v = new Validator(
      { password: '', confirm_password: 'password' },
      { password: 'sometimes', confirm_password: 'sometimes|alpha' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
    assert.equal(
      v.errors.password.message,
      v.getExistinParsedMessage({
        rule: 'sometimes',
        value: '',
        attr: 'password',
        args: [],
      }),
    );
  });

  it('should fail', async () => {
    const v = new Validator(
      { info: { password: '', confirm_password: 'password' } },
      { 'info.password': 'sometimes', 'info.confirm_password': 'sometimes|alpha' },
    );

    const matched = await v.check();

    assert.equal(matched, false);
  });

  it('should pass', async () => {
    const v = new Validator(
      { info: { password: 'password', confirm_password: 'password' } },
      { 'info.password': 'sometimes', 'info.confirm_password': 'sometimes|alpha' },
    );

    const matched = await v.check();
    assert.equal(matched, true);
  });

  it('should pass, attribute absent', async () => {
    const v = new Validator(
      {},
      { 'info.password': 'sometimes', 'info.confirm_password': 'sometimes|alpha' },
    );

    const matched = await v.check();

    assert.equal(matched, true);
  });
});