I am trying to verify that an object has a certain set of properties and that their values are of a certain type. I'd like to be able to compare an object to a template like
name: "string",
age: "number",
registered: "boolean"
and return an object with only the fields that match the template.
var object = {
name: "John McClane",
age: 45,
location: "Nakatomi Towers",
registered: "yes"
}
var document = match(object, template);
console.log(document);
/* Should return
{
name: "John McClane",
age: 45
}
*/
What are the JavaScript best practices in writing a function like this? I'm not too familiar with the built in methods and iteration so I don't want to go about this the wrong way.
inandtypeofwould probably be helpful starts - ignoring edge cases. There is no built-in feature for such a test. There are probably existing 3rd party libraries (FSVO) under the guise of 'JSON schema validation' or similar.