Skip to content

Instantly share code, notes, and snippets.

@abel-masila
Last active June 25, 2018 11:40
Show Gist options
  • Save abel-masila/29d753598cb4499682f7aca259cbc2fd to your computer and use it in GitHub Desktop.
Save abel-masila/29d753598cb4499682f7aca259cbc2fd to your computer and use it in GitHub Desktop.
var test = [
{
date_issued: '2018-05-25',
transaction_type: 'IMPORT',
policy_number: '061/100/001052/2018',
open_cover: false,
endorsement_number: '',
icc: 'ICC-A',
insured_name: 'SAYONA STEEL LTD',
insured_email: '[email protected]',
insured_tin: '103 286 883',
intermediary_name: 'MILMAR',
intermediary_tin: '100-714-078',
goods_category: 'Mineral Products',
country_of_origin: 'HK',
destination: 'Mwanza Region',
conveyance: 'SEA',
financier: '',
currency_code: 'USD',
exchange_rate_used: '2262.37',
sum_insured: '23302411',
premium: '33935.55',
VAT: '5176.609322',
unit_of_measure: 'UNITS',
quantity: '800',
cover_start_date: '2018-05-15'
},
{
date_issued: '2018-05-25',
transaction_type: 'IMPORT',
policy_number: '061/100/001051/2018',
open_cover: false,
endorsement_number: '',
icc: 'ICC-A',
insured_name: 'URHOME COMPANY LTD',
insured_email: '[email protected]',
insured_tin: '132-209-898',
intermediary_name: 'MILMAR',
intermediary_tin: '100-714-078',
goods_category: 'Base Metals And Articles Of Base Metal',
country_of_origin: 'CN',
destination: 'Dar es Salaam Region',
conveyance: 'SEA',
financier: '',
currency_code: 'USD',
exchange_rate_used: '2262.37',
sum_insured: '54941202.98',
premium: '58821.62',
VAT: '8972.789492',
unit_of_measure: 'UNITS',
quantity: '2792',
cover_start_date: '2018-05-15'
}
];
function isKeyInObject(obj, key) {
var res = Object.keys(obj).some(v => v == key);
return res;
}
test.map(data => {
console.log(isKeyInObject(data, 'VAT'));
});
@BransonGitomeh
Copy link

BransonGitomeh commented Jun 25, 2018

var test = [
    {
        date_issued: '2018-05-25',
        transaction_type: 'IMPORT',
        policy_number: '061/100/001052/2018',
        open_cover: false,
        endorsement_number: '',
        icc: 'ICC-A',
        insured_name: 'SAYONA STEEL LTD',
        insured_email: '[email protected]',
        insured_tin: '103 286 883',
        intermediary_name: 'MILMAR',
        intermediary_tin: '100-714-078',
        goods_category: 'Mineral Products',
        country_of_origin: 'HK',
        destination: 'Mwanza Region',
        conveyance: 'SEA',
        financier: '',
        currency_code: 'USD',
        exchange_rate_used: '2262.37',
        sum_insured: '23302411',
        premium: '33935.55',
        VAT: '5176.609322',
        unit_of_measure: 'UNITS',
        quantity: '800',
        cover_start_date: '2018-05-15'
    },
    {
        date_issued: '2018-05-25',
        transaction_type: 'IMPORT',
        policy_number: '061/100/001051/2018',
        open_cover: false,
        endorsement_number: '',
        icc: 'ICC-A',
        insured_name: 'URHOME COMPANY LTD',
        insured_email: '[email protected]',
        insured_tin: '132-209-898',
        intermediary_name: 'MILMAR',
        intermediary_tin: '100-714-078',
        goods_category: 'Base Metals And Articles Of Base Metal',
        country_of_origin: 'CN',
        destination: 'Dar es Salaam Region',
        conveyance: 'SEA',
        financier: '',
        currency_code: 'USD',
        exchange_rate_used: '2262.37',
        sum_insured: '54941202.98',
        premium: '58821.62',
        VAT: '8972.789492',
        unit_of_measure: 'UNITS',
        quantity: '2792',
        cover_start_date: '2018-05-15'
    }
];

const isKeyInObject = (obj, key) => obj[key] ? true : false

const checkArray = (test, key) => {
    let valid = true

    test.map(item => {
        if (!isKeyInObject(item, key))
            valid = false
    })

    return valid
}

console.log(checkArray(test, 'VAT'))

@BransonGitomeh
Copy link

with joi

const Joi = require('joi');

var test = [
    {
        date_issued: '2018-05-25',
        transaction_type: 'IMPORT',
        policy_number: '061/100/001052/2018',
        open_cover: false,
        endorsement_number: '',
        icc: 'ICC-A',
        insured_name: 'SAYONA STEEL LTD',
        insured_email: '[email protected]',
        insured_tin: '103 286 883',
        intermediary_name: 'MILMAR',
        intermediary_tin: '100-714-078',
        goods_category: 'Mineral Products',
        country_of_origin: 'HK',
        destination: 'Mwanza Region',
        conveyance: 'SEA',
        financier: '',
        currency_code: 'USD',
        exchange_rate_used: '2262.37',
        sum_insured: '23302411',
        premium: '33935.55',
        VAT: '5176.609322',
        unit_of_measure: 'UNITS',
        quantity: '800',
        cover_start_date: '2018-05-15'
    },
    {
        date_issued: '2018-05-25',
        transaction_type: 'IMPORT',
        policy_number: '061/100/001051/2018',
        open_cover: false,
        endorsement_number: '',
        icc: 'ICC-A',
        insured_name: 'URHOME COMPANY LTD',
        insured_email: '[email protected]',
        insured_tin: '132-209-898',
        intermediary_name: 'MILMAR',
        intermediary_tin: '100-714-078',
        goods_category: 'Base Metals And Articles Of Base Metal',
        country_of_origin: 'CN',
        destination: 'Dar es Salaam Region',
        conveyance: 'SEA',
        financier: '',
        currency_code: 'USD',
        exchange_rate_used: '2262.37',
        sum_insured: '54941202.98',
        premium: '58821.62',
        VAT: '8972.789492',
        unit_of_measure: 'UNITS',
        quantity: '2792',
        cover_start_date: '2018-05-15'
    }
];


const schema = Joi
    .array()
    .items(Joi.object()
        .keys({
            VAT: Joi.required(),
            destination: Joi.required(),
            test: Joi.required(),
        }))

// Wrap in a try catch if using async await, 
Joi.validate(test, schema)
    .then(_ => console.log("valid"))
    .catch(_ => console.log("invalid"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment