Releases: trojs/validator
Releases · trojs/validator
Publish package to GitHub
2.1.5 Publish to GitHub
Add url and date validation
Merge pull request #51 from hckrnews/feature/add-url-validation Add url and date validation
Invalid fields
You can now find the wrong fields with .errors
Optional key can be set to the key
You can set the question mark at the key, so more options are possible with the types, e.g. if the type definition isn't a string.
{
first: String,
"?second": String,
}
You can also validate an array of items
Example usage:
const persons = [
{
name: "James",
age: 25,
siblings: ["Johnnathan"],
metaData: {},
active: true,
address: {
street: "Streetname",
number: 1,
postalCode: "1234AB",
city: "City",
country: "Somewehere"
},
companies: [
{ name: "Example company 1", website: "https://hckr.news" }
{ name: "Example company 2" }
]
}
];
validator.validateAll(persons);
Now you can add sub array's with objects
Example multi level schema:
const personSchema = {
name: "string",
age: "number",
siblings: "array",
metaData: "?object",
active: "boolean",
address: {
street: "string",
number: "number",
postalCode: "string",
city: "string",
country: "string"
},
companies: {
name: "string",
website: "?string"
}
};
Example valid data for the person schema:
const personObj = {
name: "James",
age: 25,
siblings: ["Johnnathan"],
metaData: {},
active: true,
address: {
street: "Streetname",
number: 1,
postalCode: "1234AB",
city: "City",
country: "Somewehere"
},
companies: [
{ name: "Example company 1", website: "https://hckr.news" }
{ name: "Example company 2" }
]
}