-1

I have to write a javascript function which can validate data according to this kind of schema. How should i go about this? Can i use json schema for this. That does'nt work for fucnctions so how should i solve this? Is there any native js way to go about this?

const userSchema = {
    name: {
        fn: () => String,
        option: { default: 'Abhi', enum: ['John', 'Rick', 'Dan'] }
    },
    phone: {
        fn: () => Number,
        option: {}
    },
    cart: {
        fn: () => [
            {
                id: {
                    fn: () => String,
                    option: {}
                },
                count: {
                    fn: () => Number,
                    option: {}
                }
            }
        ],
        option: {}
    },
    address: {
        contactName: {
            fn: () => String,
            option: {}
        },
        detailAddress: {
            line1: {
                fn: () => String,
                option: {}
            },
            line2: {
                fn: () => String,
                option: {}
            },
            line3: {
                fn: () => String,
                option: {}
            }
        },
        pin: {
            fn: () => Number,
            option: {}
        },
        country: {
            fn: () => String,
            option: {}
        }
    }
};
const user = {
    name: 'Ric',
    phone: 6610592314,
    address: {
        contactName: 'Bitu',
        detailAddress: {
            line1: 'Colony: 2/A',
            line2: 'Q. No.: 3-018',
            line3: 'Near IT store'
        },
        pin: 770017,
        country: 'India'
    },
    cart: [
        {
            id: 'newId',
            count: 2
        }
    ]
};
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71

2 Answers2

0

Joi is a good library for this. Flow and TypeScript also work at compile-time, but won't help you with runtime validation. Joi's function validation isn't perfect, but there's a workaround for argument validation here.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
0

I've been working lately on a JS Runtime Scheme Validation Tool, creatively called Types.

It is still at its early stages (powerful features implementations are expected in the near future), but I think it could fit your needs. Currently it is also able to handle circular schemes.

To install it, do yarn add @codistica/types or npm i @codistica/types.

Then, here you have an usage example (documentation is still work in progress):

import {Types} from '@codistica/types';

const myFunctionSchema = new Types({
    argA: {type: '!undefined'},
    argB: {type: 'Function'},
    argC: {
        type: 'Object',
        def: {
            propA: {type: 'number', min: 0, max: 20, def: 10},
            propB: {type: 'boolean', def: false},
            propC: {type: ['Function', 'null'], def: null},
            propD: {type: 'Array<number>', def: [0, 1, 2]}
        }
    }
});

function myFunction(argA, argB, argC) {
    ({argA, argB, argC} = myFunctionSchema.validate({
        argA,
        argB,
        argC
    }));

    if (!myFunctionSchema.isValid()) {
        return;
    }
}

export {myFunction};

If you would like to give it a try, I would be glad to assist you in any possible way. The above example is really an underestimation. Maybe you could also give us some useful feedback. Please let me know.

Ernesto Stifano
  • 3,027
  • 1
  • 10
  • 20