PHP Classes

File: test/js/test.js

Recommend this page to a friend!
  Classes of Nikos M.  >  Formal PHP Validation Library  >  test/js/test.js  >  Download  
File: test/js/test.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Formal PHP Validation Library
Validate a set values with support to type casting
Author: By
Last change: v.1.2.0

* correctly handle (wildcard) missing keys in typecast/validate
* correctly handle some edge cases when input data is not array or object
* update tests
Date: 1 month ago
Size: 2,713 bytes
 

Contents

Class file image Download
"use strict";
var Formal = require('../../src/js/Formal.js');

async function test()
{
    var formal = (new Formal())
            .option('WILDCARD', '*') // default
            .option('SEPARATOR', '.') // default
            .option('break_on_first_error', false) // default
    ;
    var formdata = {
        'foo' : '',
        'moo' : [
                {'choo' : 1},
                {'choo' : 2},
                {'choo' : 3},
        ],

        'soo' : [
                {
                    'boo' : 1,
                    'xoo' : 'a'
                },
                {
                    'boo' : 2,
                    'xoo' : 'b'
                },
                {
                    'boo' : 3,
                    'xoo' : 'c'
                },
        ],

        'koo' : [
            '',
            '',
            '',
        ],

        'num' : [
            '0.1',
            '1.2',
        ],

        'date' : [
            '2012-11-02',
            '20-11-02',
        ],
    };
    var data = await formal
        .option('defaults', {
            'foo' : 'bar',
            'moo.*.foo' : 'bar',
            'koo.*' : 'bar'
        })
        .option('typecasters', {
            'koo.*.foo' : Formal.typecast('str'),
            'num.*' : Formal.typecast('composite', [Formal.typecast('float'), Formal.typecast('clamp', [0.0, 1.0])
        ])})
        .option('validators', {
            'foo.*' : Formal.validate('required'),
            'foo.*.foo' : Formal.validate('required'),
            'moo.*.foo' : Formal.validate('required'),
            'koo.*.foo' : Formal.validate('optional', Formal.validate('required')),
            'date.*' : Formal.validate('match', Formal.datetime('Y-m-d'), '"{key}" should match {args} !'),
            'date.0' : Formal.validate('eq', Formal.field('date.1'))
        })
        .process(formdata);
    var err = formal.getErrors();

    console.log(JSON.stringify(formdata, null, 4));

    console.log(JSON.stringify(data, null, 4));

    console.log(err.join("\n"));

    console.log(JSON.stringify(formal.get('soo.1.boo', 'default', formdata), null, 4));
    console.log(JSON.stringify(formal.get('soo.*.boo', 'default', formdata), null, 4));
    console.log(JSON.stringify(formal.get('soo.*.*', 'default', formdata), null, 4));
    console.log(JSON.stringify(formal.get('soo.1.koo', 'default', formdata), null, 4));
    console.log(JSON.stringify(formal.get('soo.*.koo', 'default', formdata), null, 4));
    console.log(JSON.stringify(formal.get('soo.koo.1', 'default', formdata), null, 4));
    console.log(JSON.stringify(formal.get('soo.koo.*', 'default', formdata), null, 4));
}

console.log('Formal.VERSION ' + Formal.VERSION);
test();
For more information send a message to info at phpclasses dot org.