-4

Use case: I have an http request represented by an object and I want to check the fields of the object before I make an http call with the request(i.e. makeHttpCall(request)). For example, given the object below:

{ "classic": false, "billing-account": "1234567890", "device": "123456" } 

I want to validate the following fields from the object:

  • "classic" is a bool true/false
  • "billing-account" is a string with length 9
  • "device" value is a string with length 6

I want to be able to write a config that can be used to validate the fields, similar PropTypes or Formik. For example:

{ "classic": boolean "billing-account": string.length===9 "device": string.length===9 } 

UPDATE: yup is what I was looking for https://github.com/jquense/yup

5
  • 1
    Show us what you have tried. SO isn't a free code writing service. The objective here is for you to post your attempts to solve your own issue and others help when they don't work as expected. See How to Ask and minimal reproducible example Commented Aug 2, 2021 at 22:49
  • 1
    Common, well-tested, and very popular options include joi and validatorjs. Commented Aug 2, 2021 at 23:00
  • You need to use functions in your example, like classic: x => typeof x === "boolean". Next you can iterate over the keys and pass the values to the validator functions. Commented Aug 2, 2021 at 23:04
  • thank you @jarmond, I will look into these Commented Aug 2, 2021 at 23:07
  • 2
    Duplicate: Javascript - elegant way to check object has required properties Commented Aug 2, 2021 at 23:14

2 Answers 2

1

You can make a function in order to validate each key as an if statement:

const request1 = { "classic": false, "billing-account": "1234567890", "device": "123456" } const request2 = { "classic": false, "billing-account": "123456789", "device": "123456" } console.log(validate(request1)); // false console.log(validate(request2)); // true function validate(obj) { return typeof obj["classic"] === "boolean" && obj["billing-account"].length === 9 && obj["device"].length === 6; }

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is something I was looking for
This breaks for "billing-account": 123456789
0

You can create a conditions object that has function attributes, then loop through the attributes and check the corresponding attribute in the object to validate.

const obj = { "classic": false, "billing-account": "123456789", "device": "123456" } const conditions = { "classic": e => typeof e == "boolean", "billing-account": e => e.length == 9, "device": e => e.length == 6 } function validate(s) { var isValid = true; Object.keys(s).forEach(e => isValid = !conditions[e](s[e]) ? false : isValid); return isValid; } console.log(validate(obj));

4 Comments

@ChrisG What made you say it breaks then?
@ChrisG That's not mentioned in the question, and that's not even the main thing the OP is having trouble with. I am assuming the asker already has the proper validation functions written, but they are only looking for how you can combine them together.
@ChrisG Also, it doesn't break, since undefined is not equal to 9.
That is true, but it'll give a false result for something that isn't a string but has a length property of value 9. To make sure it's a string, you need to use typeof e === "string" no matter how you slice it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.