repocrypts / Validator

Client-side javascript validator library ports from Laravel 5.2
MIT License
46 stars 22 forks source link

Encapsulation is not working #29

Open BackdoorTech opened 4 years ago

BackdoorTech commented 4 years ago

this is my data structure


const file = {
  'MNDTImf:InputMandateFile': {
    '@xmlns:MNDTImf': 'urn:MNDTImf:xsd:$MPEMndt',
    'MNDTImf:SndgInst': '',
    'MNDTImf:RcvgInst': '',
    'MNDTImf:FileRef': '',
    'MNDTImf:SrvcId': '',
    'MNDTImf:TstCode': '',
    'MNDTImf:FType': '',
    'MNDTImf:FDtTm': '',
    'MNDTImf:Num009CAPBlk': '',
    'MNDTImf:Num009SAPBlk': '',
    'MNDTImf:Num012CAPBlk': '',
    'MNDTImf:Num012SAPBlk': '',
    'MNDTImf:Num010Blk': '',
    'MNDTImf:Num011Blk': '',
    'MNDTImf:Pain011': '',
    'MNDTImf:Pain009SAP': {
      '@xmlns': 'urn:iso:std:iso:20022:tech:xsd:pain.009.001.05',
      'GrpHdr': {
        'MsgId': '',
        'CreDtTm': '',
        'InstgAgt':{
          'FinInstnId':{
            'BICFI': '' // BIC of the sender
          }
        },
        'InstdAgt': {
          'FinInstnId': {
            'BICFI': '' // BIC of the receiver
          }
        }
      },
      'Mndt': {
        'MndtId': '',
        'MndtReqId': '',
        'Tp': '',
        'Ocrncs': {
          'SeqTp': '',
          'Frqcy': '',
          'FrstColltnDt': '',
          'FnlColltnDt': ''
        },
        'TrckgInd': '',
        'MaxAmt': { // attribuite and value
          '@Ccy': 'AOA'
        },
        'Cdtr': {
          'Nm': '',
          'Id': {
            'OrgId': {
              'Othr': {
                'Id': ''
              }
            }
          }
        },
        'CdtrAcct':{
          'id': {
            'IBAN': ''
          }
        },
        'CdtrAgt': {
          'FinInstnId': {
            'BICFI': ''
          }
        },
        'Dbtr': {
          'Nm': '',
          'Id': {
            'OrgId': {
              'Othr': {
                'Id': ''
              }
            },
          }
        },
        'DbtrAcct': {
          'Id': ''
        },
        'DbtrAgt': {
          'FinInstnId':{
            'BICFI':''
          }
        },
        'DbtrAcct': {
          'Id': {
            'IBAN': ''
          }
        },
        'DbtrAgt': {
          'FinInstnId': {
            'BICFI': ''
          }
        },
        'RfrdDoc': {
          'CdtrRef': '',
          'RltdDt': ''
        }
      }
    }
  }
};

const v = Valid.make( file, rules);
TypeError: e.forEach is not a function
    at i.value (C:\Users\peter.maquiran\Desktop\TEST\node_modules\Validator\dist\Validator.js:1:4117)
    at i.value (C:\Users\peter.maquiran\Desktop\TEST\node_modules\Validator\dist\Validator.js:1:3914)
    at new i (C:\Users\peter.maquiran\Desktop\TEST\node_modules\Validator\dist\Validator.js:1:3503)
    at Function.value (C:\Users\peter.maquiran\Desktop\TEST\node_modules\Validator\dist\Validator.js:1:18595)
    at Object.<anonymous> (C:\Users\peter.maquiran\Desktop\TEST\index.js:29:17)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
BackdoorTech commented 4 years ago

Looping through each element is not that hard.

BackdoorTech commented 4 years ago

i will let you know when i finish

BackdoorTech commented 4 years ago

Not the best way but works just fine;

const Validator = require('Validator');

var data = {
  name: 'John Doe',
  test:{
    birthday: '20000000000000'
  },
  company: 'Example Co.',
};

var rules = {
  name: 'required',
  test:{
    birthday: 'required'
  },
  company: ['required', 'string']
};

function convertObj2SpaceName(MObj) {

  const $type = (e) => (e.constructor)
  var nameSpace = [];
  var rlt = {};

  const loop = (obj) => {
    // get object indexs
    const entries = Object.entries(obj);

    for (const [i, val] of entries) {

      nameSpace.push(i);
      // if object
      if ($type(val) === Object) {
        loop(val);
      } else { // if not object
        rlt[nameSpace.join('/')] = val
      }

      nameSpace.pop();
    }

    return rlt;
  }

  return loop(MObj);
}

var data = convertObj2SpaceName(data);
var rules = convertObj2SpaceName(rules);

const v = Validator.make(data, rules);

if (v.fails()) {
    const errors = v.getErrors();
    console.log(errors);
} else {
  console.log('good!')
}
jfstn commented 4 years ago

Hello 👋

After checking the issue it seems that currently this package does not support validation of nested objects.

Two different approachs can be pursued, we either discard nested objects from validation or we support nested objects validation. @PeterDias55 if you can solve the issue in either way feel free to open a PR.

It will be some time until I can fix it.

Thanks, João Faustino

BackdoorTech commented 3 years ago

Ok, then i will open a PR as soon as I finish the implementation

Thanks, Peter Dias Dos Santos

jfstn commented 3 years ago

Any news on this @PeterDias55 ?

BackdoorTech commented 3 years ago

No i don't. i won't be able to finish it my priority has changed.

my apology @jfstn