A helpful example on parsers and validators
Niffy opened this issue · comments
Paul Robinson commented
Like me and you struggled to read the docs and the code to understand how to use parsers and validators, I've thrown together an example to help people get their head around how to write schemas with them.
const schema = require('schm')
const moment = require('moment')
const dateDefault = {
date: moment().toISOString(),
month: moment().format('MMMM'),
year: moment().format('YYYY')
}
const extraStuff = prevSchema => prevSchema.merge({
parsers: {
transformDate: (formedDefaultOrPassedRequest, propValue, schemaProp, schema, request, someOtherStuff) => {
console.log('----- parsers transformDate -----')
// This is the default formed or what is in the request
// The formed default is only passed if the request does not contain the schemaProp
console.log('formedDefaultOrPassedRequest', formedDefaultOrPassedRequest)
// transformDate value, in this case it is true
console.log('propValue', propValue)
// In this case it is occ, as the transformDate property is inside the occ object
console.log('schemaProp', schemaProp)
// the schema for this property (occ), this may contain the default if set
console.log('schema', schema)
// What has been requested to be parsed
console.log('request', request)
// Here we're going to just return some junk because we can
// We could do some manipulation
return {
date: "aaaa",
month: 'eee',
year: 'ddd'
}
}
},
validators: {
checkDate: (valueToCheck, options, schemaProp, schema, request, someOtherStuff) => {
console.log('----- validators checkDate -----')
// This is the value to check
console.log('valueToCheck', valueToCheck)
// What is the value of checkDate defined on the schema
console.log('options', options)
// In this case it is occ, as the checkDate property is inside the occ object
console.log('schemaProp', schemaProp)
// the schema for this property (occ), this may contain the default if set
console.log('schema', schema)
// What has been requested to be parsed/validated
console.log('request', request)
// Be careful, the validator will still run if the options said checkDate is false
// Lets check if the date is a value ISO8601 date
return {
valid: moment(valueToCheck.date, 'YYYY-MM-DDTHH:mm:ssZ').isValid(),
message: 'Not a valid date',
}
}
}
})
const someSchema = schema({
name: { type: String, default: null },
occ: {
type: {
date: { type: String, default: moment().toISOString() },
month: { type: String, default: moment().format('MMMM') },
year: { type: String, default: moment().format('YYYY') },
},
default: dateDefault,
transformDate: true,
checkDate: true
}
}, extraStuff)
var req = {
name: "MrAwesome",
occ: {
date: "2016-12-01T13:30:30.705Z",
month: "December",
year: "2016"
}
}
const valid = schema.validate(req, someSchema)
.then((res) => {
console.log('----- result -----')
console.log(res)
})
.catch((err) => {
console.log('----- error -----')
console.log(err)
})
results in
----- parsers transformDate -----
formedDefaultOrPassedRequest { date: '2016-12-01T13:30:30.705Z',
month: 'December',
year: '2016' }
propValue true
schemaProp occ
schema { type:
{ date:
{ type: [Function: String],
default: '2018-06-28T10:23:59.877Z' },
month: { type: [Function: String], default: 'June' },
year: { type: [Function: String], default: '2018' } },
default: { date: '2018-06-28T10:23:59.873Z', month: 'June', year: '2018' },
transformDate: true,
checkDate: true }
request { name: 'MrAwesome',
occ:
{ date: '2016-12-01T13:30:30.705Z',
month: 'December',
year: '2016' } }
----- validators checkDate -----
valueToCheck { date: 'aaaa', month: 'eee', year: 'ddd' }
options { optionValue: true }
schemaProp occ
schema { type:
{ date:
{ type: [Function: String],
default: '2018-06-28T10:23:59.877Z' },
month: { type: [Function: String], default: 'June' },
year: { type: [Function: String], default: '2018' } },
default: { date: '2018-06-28T10:23:59.873Z', month: 'June', year: '2018' },
transformDate: true,
checkDate: true }
request { name: 'MrAwesome',
occ: { date: 'aaaa', month: 'eee', year: 'ddd' } }
----- error -----
[ { param: 'occ',
validator: 'checkDate',
checkDate: true,
message: 'Not a valid date' } ]