USE TDD Practices
Validator
classSchema
class
The Validator
class is used to validate and cast a single field in an object.
const nameValidator = new Validator('name', {
type: String,
required: true
});
const ageValidator = new Validator('age', {
type: String,
required: true
});
const colorValidator = new Validator('color', {
type: String,
required: true
});
const dog = {
name: 'spot',
age: '5',
weight: '20 lbs'
};
nameValidator.validate(dog) // returns 'spot'
age.validate(dog) // returns 5
colorValidator.validate(dog) // throws error
- Test that
validate
method can take an object and return a fields value - Test that
validate
method can take an object and throw an error - Test all permutations:
- required and field missing
- required and field there but wrong type
- required and field there and right type
- not required and field missing
- not required and field there but wrong type
- not required and field there and right type
Validator
is initialize with two parameters:field
andconfiguration
- use your
getCaster
method from yesterdays lab to get the correctcastTo*
function based ontype
from theconfiguration
parameter passed in
- use your
Validator
has avalidate
method that takes an object to validate- make sure the field is there if it is required
- use the
castTo*
function to cast the field
The Schema
class is used to validate and cast an entire object.
const schema = new Schema({
dog: {
type: String,
required: true
},
age: {
type: Number,
required: true
},
weight: {
type: String
}
});
const spot = {
name: 'spot',
age: 5,
weight: '20 lbs'
};
const rover = {
name: 'rover',
age: '10'
};
const who = {
age: 'hi'
};
schema.validate(spot) // returns { name: 'spot', age: 5, weight: '20 lbs' }
schema.validate(rover) // returns { name: 'rover', age: 10 };'
schema.validate(who) // throws a errors about name being required and age not being a number
Don't retest functionality already tested by your Validator tests
- Test that
validate
method returns the object with all fields cast - Test that
validate
method throws an error if the object doesn't follow the schema
Schema
is initialized with an object defining a schema (schemaDefinition
)- use the object to create an array of
Validator
s for each key/value inschemaDefinition
and store the array inthis.validators
Schema
has avalidate
method that takes an object - invoke the
validate
method for eachValidator
inthis.validate
"
- use the object to create an array of