Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
How to validate object with schema?

This is the main file where I have schema and values. I have validation-type which returns true if type is string ect. Then there is validation file where I should validate values with schema.

import * as ValidationTypes from './lib/validation-types'
    import {validate} from "./lib/validation";

const schema = {
  name: ValidationTypes.string,
  age: ValidationTypes.number,
  extra: ValidationTypes.any,
};

const values = {
  name: "John",
  age: "",
  extra: false,
};

let result = validate(schema, values); //return array of invalid keys
console.log(result.length === 0 ? "valid" : "invalid keys: " + result.join(", "));
//invalid keys: age

validation-type

export function string(param) {
    return typeof param === "string"
  }

  export function number(param) {
    return typeof param === "number"
  }

  export function any(param) {
    return true;
  }

and this is validation.js

export function validate(schema, values) {
  let invalidValues = []


}

And I am stuck, don't know how to continue with validate function.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

What you're looking for is somewhat like following code block:

export function validate(schema, values) {
  let invalidValues = [];

  // Make sure schema is an object
  if (typeof schema !== "object") {
    throw new Error("Schema must be an object");
  }

  // Loop over each key of the schema
  Object.keys(schema).forEach((schemaKey) => {
    // Get the validation function associated with
    // the specific key of the validation schema
    const validationFn = schema[schemaKey];

    // Sanity check. Check if the variable associated
    // with the schemaKey is a function or not.
    if (typeof validationFn !== "function") {
      throw new Error("Invalid validation function");
    }

    // Get the validation result of the schemaKey
    // through it's validation function.
    // The schema key is also used in the values passed
    // to this function.
    const validationResult = validationFn(values[schemaKey]);

    // Check if the validation is true
    if (!validationResult) {
      // If the validation is not true
      // Push the error message
      invalidValues.push(
        // `schemaKey` is the name of the variable
        // `validationFn.name` gives the name of the validation function
        // which in this case is associated with variable type
        `[${schemaKey}] must be of type [${validationFn.name}]`
      );
    }
  });

  return invalidValues;
}

You can refer this sandbox for better understanding.

Basically, the code is using the name of the validation function to give better validation error results. You can modify the error result pushed to invalidValues to get the type of error result you want.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!