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/objects.js
const assert = require('assert');

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

describe('Objects', () => {
  describe('Single Level', () => {
    it('should pass with top level required', async () => {
      const v = new Validator(
        {
          product: {
            id: '1', name: 'Product', price: '12.50', active: 'yes',
          },
        },
        {
          product: 'required|object',
          'product.id': 'integer',
          'product.name': 'string',
          'product.price': 'numeric',
          'product.active': 'string',
        },
        {
          product: 'The given product is invalid :value.',
        },
      );

      const matched = await v.check();

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

    it('should fail with child level required', async () => {
      const v = new Validator(
        {
          product: {
            id: 1, name: '', price: '', active: 'yes',
          },
        },
        {
          product: 'required|object',
          'product.id': 'required|integer',
          'product.name': 'required|string',
          'product.price': 'required|numeric',
          'product.active': 'required|string',
        },
        {
          product: 'The given product is invalid :value.',
        },
      );

      const matched = await v.check();

      assert.equal(matched, false);
      v.errors.should.have.keys('product.name', 'product.price');
    });
  });

  describe('Deep Level', () => {
    it('should pass with top level required', async () => {
      const v = new Validator(
        {
          product: {
            id: '1',
            name: 'Product',
            price: '12.50',
            weight: { unit: 'gram', value: 100 },
          },
        },
        {
          product: 'required|object',
          'product.id': 'integer',
          'product.name': 'string',
          'product.price': 'numeric',
          'product.weight': 'object',
          'product.weight.unit': 'in:gram',
          'product.weight.value': 'numeric',
        },
        {
          product: 'The given product is invalid :value.',
        },
      );

      const matched = await v.check();

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

    it('should fail with child level required', async () => {
      const v = new Validator(
        {
          product: {
            id: '1',
            name: '',
            price: '12.50',
            weight: { unit: 'kilo', value: '' },
          },
        },
        {
          product: 'required|object',
          'product.id': 'required|integer',
          'product.name': 'required|string',
          'product.price': 'required|numeric',
          'product.weight': 'required|object',
          'product.weight.unit': 'required|in:gram',
          'product.weight.value': 'required|numeric',
        },
        {
          product: 'The given product is invalid :value.',
        },
      );

      const matched = await v.check();

      assert.equal(matched, false);

      v.errors.should.have.keys('product.name', 'product.weight.unit', 'product.weight.value');
    });

    it('should fail with child level missing', async () => {
      const v = new Validator(
        {
          product: {
            id: '1',
            name: '',
            price: '12.50',
          },
        },
        {
          product: 'required|object',
          'product.id': 'required|integer',
          'product.name': 'required|string',
          'product.price': 'required|numeric',
          'product.weight': 'required|object',
          'product.weight.unit': 'required|in:gram',
          'product.weight.value': 'required|numeric',
        },
        {
          product: 'The given product is invalid :value.',
        },
      );

      const matched = await v.check();

      assert.equal(matched, false);

      v.errors.should.have.keys('product.name', 'product.weight', 'product.weight.unit', 'product.weight.value');
    });
  });
});