#Working on a schema validation tool in JavaScript

8 messages · Page 1 of 1 (latest)

obsidian finch
#

Here's a usage example of what this project does

#
const Parser = require('./src/Parser.js');

const parser = new Parser();

const typeString = `{
    company: {
        name: string,
        industry: string,
        establishedYear: number min = 1900 max = 2023,
        address: {
            street: string,
            city: string,
            state: string,
            zipCode: string pattern = /^\\d{5}(-\\d{4})?$/
        },
        employees: [{
            id: string,
            personalInfo: {
                firstName: string,
                lastName: string,
                age: number min = 18 max = 65,
                email: string pattern = /^[\\w-.]+@([\\w-]+\\.)+[\\w-]{2,4}$/,
                phoneNumbers: [string pattern = /^\\+?\\d{10,15}$/]
            },
            jobInfo: {
                title: string,
                department: string,
                hireDate: string pattern = /^\\d{4}-\\d{2}-\\d{2}$/,
                salary?: number min = 30000 max = 200000
            }
        }] <0..100>,
        projects: [{
            name: string,
            startDate: string pattern = /^\\d{4}-\\d{2}-\\d{2}$/,
            endDate?: string pattern = /^\\d{4}-\\d{2}-\\d{2}$/,
            team: [{
                employeeId: string,
                role: string
            }]
        }]
    },
    mainOffice?: {
        country: string,
        coordinates: {
            latitude: number min = -90 max = 90,
            longitude: number min = -180 max = 180
        }
    }
}`;```
#
const data = {
    company: {
        name: 'Innovatech Solutions',
        industry: 'Technology',
        establishedYear: 2010,
        address: {
            street: '123 Innovation Drive',
            city: 'Techville',
            state: 'CA',
            zipCode: '94043-1234'
        },
        employees: [
            {
                id: 'emp001',
                personalInfo: {
                    firstName: 'Alice',
                    lastName: 'Johnson',
                    age: 30,
                    email: '[email protected]',
                    phoneNumbers: ['+12345678901', '+19876543210']
                },
                jobInfo: {
                    title: 'Software Engineer',
                    department: 'Engineering',
                    hireDate: '2015-08-01',
                    salary: 120000
                }
            },
            {
                id: 'emp002',
                personalInfo: {
                    firstName: 'Bob',
                    lastName: 'Smith',
                    age: 40,
                    email: '[email protected]',
                    phoneNumbers: ['+10987654321']
                },
                jobInfo: {
                    title: 'Project Manager',
                    department: 'Management',
                    hireDate: '2012-03-15',
                    salary: 150000
                }
            }
        ],
        projects: [
            {
                name: 'Project Alpha',
                startDate: '2022-01-01',
                endDate: '2023-01-01',
                team: [
                    {
                        employeeId: 'emp001',
                        role: 'Lead Developer'
                    },
                    {
                        employeeId: 'emp002',
                        role: 'Project Manager'
                    }
                ]
            },
            {
                name: 'Project Beta',
                startDate: '2023-02-01',
                team: [
                    {
                        employeeId: 'emp002',
                        role: 'Project Manager'
                    }
                ]
            }
        ]
    },
    mainOffice: {
        country: 'USA',
        coordinates: {
            latitude: 37.7749,
            longitude: -122.4194
        }
    }
};


const type = parser.parseType(typeString); //parsing the type string takes about 7ms on my machine but it only needs to be done once

const start = Date.now();

console.log(type.validate(data)); // true

const end = Date.now();

console.log(`Validation took ${end - start}ms`); //This validation takes 1ms on my machine, the same schema in Joi took 3ms```
#

still a work in progress but I thought it was interesting

#

if anyone has any questions about it feel free to ask

open plume
#

learning about lexing and parsing kinda cool

#

I guess one thing you could look at is having the type be defined in the code (as opposed to a big string), kind of like zod

obsidian finch