record value coersion - untested

This commit is contained in:
Michael Shanks 2020-10-02 14:14:58 +01:00
parent 8e528a6160
commit 3e1e865bb4
1 changed files with 53 additions and 0 deletions

View File

@ -33,6 +33,8 @@ exports.patch = async function(ctx) {
const model = await db.get(record.modelId) const model = await db.get(record.modelId)
const patchfields = ctx.request.body const patchfields = ctx.request.body
coersceRecordValues(record, model)
for (let key in patchfields) { for (let key in patchfields) {
if (!model.schema[key]) continue if (!model.schema[key]) continue
record[key] = patchfields[key] record[key] = patchfields[key]
@ -71,6 +73,8 @@ exports.save = async function(ctx) {
const model = await db.get(record.modelId) const model = await db.get(record.modelId)
coersceRecordValues(record, model)
const validateResult = await validate({ const validateResult = await validate({
record, record,
model, model,
@ -218,3 +222,52 @@ async function validate({ instanceId, modelId, record, model }) {
} }
return { valid: Object.keys(errors).length === 0, errors } return { valid: Object.keys(errors).length === 0, errors }
} }
function coersceRecordValues(record, model) {
for (let [key, value] of Object.entries(record)) {
const field = model.schema[key]
if (!field) continue
const mapping = Object.prototype.hasOwnProperty.call(
TYPE_TRANSFORM_MAP,
value
)
? TYPE_TRANSFORM_MAP[field.type][value]
: TYPE_TRANSFORM_MAP[field.type].parse
record[key] = typeof mapping === "function" ? mapping(value) : mapping
}
}
const TYPE_TRANSFORM_MAP = {
string: {
"": "",
[null]: "",
[undefined]: undefined,
parse: s => s.toString(),
},
number: {
"": null,
[undefined]: undefined,
parse: n => parseFloat(n),
},
datetime: {
"": null,
[undefined]: undefined,
[null]: null,
parse: d => new Date(d).getTime(),
},
attachments: {
[null]: [],
[undefined]: undefined,
parse: a => a,
},
boolean: {
[null]: null,
[undefined]: undefined,
parse: b => {
if (b === "false") return false
if (b === "true") return true
return b
},
},
}