Reputation: 178
I making a simple object validator. In my contructor function for my class the first property will be the schema validation and the second property the object to validate. For the first property anyone know a way to define the structure like this?
{
prop1: { type: "string", message: "some message" },
prop2: { type: "string", message: "some message" },
prop3: { type: "string", message: "some message" },
prop4: { type: "string", message: "some message" },
prop5: { type: "string", message: "some message" },
....
}
The only thing a not getting here is how to create a dynamic object with a different names for the properties and make sure all of then will be a property with the type: string
and message: string
as properties. If don't have any form to do this, I have a way to guarantee all the properties have the structure?
Upvotes: 0
Views: 322
Reputation: 275867
Added an annotation and an example:
const example: {
[index: string]: { type: string, message: string }
} = {
prop1: { type: "string", message: "some message" },
prop2: { type: "string", message: "some message" },
prop3: { type: "string", message: "some message" },
prop4: { type: "string", message: "some message" },
prop5: { type: "string", message: "some message" },
};
Upvotes: 2