You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
})
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.
results in
The text was updated successfully, but these errors were encountered: