a few changes from code review

This commit is contained in:
Michael Shanks 2020-10-06 21:37:10 +01:00
parent 0b6d0f2b19
commit bbff4db649
1 changed files with 16 additions and 20 deletions

View File

@ -1,6 +1,7 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const validateJs = require("validate.js") const validateJs = require("validate.js")
const { getRecordParams, generateRecordID } = require("../../db/utils") const { getRecordParams, generateRecordID } = require("../../db/utils")
const { cloneDeep } = require("lodash")
const MODEL_VIEW_BEGINS_WITH = "all_model:" const MODEL_VIEW_BEGINS_WITH = "all_model:"
@ -31,11 +32,11 @@ validateJs.extend(validateJs.validators.datetime, {
exports.patch = async function(ctx) { exports.patch = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.instanceId)
const record = await db.get(ctx.params.id) let record = await db.get(ctx.params.id)
const model = await db.get(record.modelId) const model = await db.get(record.modelId)
const patchfields = ctx.request.body const patchfields = ctx.request.body
coerceRecordValues(record, model) record = coerceRecordValues(record, model)
for (let key in patchfields) { for (let key in patchfields) {
if (!model.schema[key]) continue if (!model.schema[key]) continue
@ -66,7 +67,7 @@ exports.patch = async function(ctx) {
exports.save = async function(ctx) { exports.save = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.instanceId)
const record = ctx.request.body let record = ctx.request.body
record.modelId = ctx.params.modelId record.modelId = ctx.params.modelId
if (!record._rev && !record._id) { if (!record._rev && !record._id) {
@ -75,7 +76,7 @@ exports.save = async function(ctx) {
const model = await db.get(record.modelId) const model = await db.get(record.modelId)
coerceRecordValues(record, model) record = coerceRecordValues(record, model)
const validateResult = await validate({ const validateResult = await validate({
record, record,
@ -236,19 +237,20 @@ async function validate({ instanceId, modelId, record, model }) {
return { valid: Object.keys(errors).length === 0, errors } return { valid: Object.keys(errors).length === 0, errors }
} }
function coerceRecordValues(record, model) { function coerceRecordValues(rec, model) {
const record = cloneDeep(rec)
for (let [key, value] of Object.entries(record)) { for (let [key, value] of Object.entries(record)) {
const field = model.schema[key] const field = model.schema[key]
if (!field) continue if (!field) continue
const mapping = Object.prototype.hasOwnProperty.call(
TYPE_TRANSFORM_MAP[field.type],
value
)
? TYPE_TRANSFORM_MAP[field.type][value]
: TYPE_TRANSFORM_MAP[field.type].parse
record[key] = typeof mapping === "function" ? mapping(value) : mapping // eslint-disable-next-line no-prototype-builtins
if (TYPE_TRANSFORM_MAP[field.type].hasOwnProperty(value)) {
record[key] = TYPE_TRANSFORM_MAP[field.type][value]
} else if (TYPE_TRANSFORM_MAP[field.type].parse) {
record[key] = TYPE_TRANSFORM_MAP[field.type].parse(value)
}
} }
return record
} }
const TYPE_TRANSFORM_MAP = { const TYPE_TRANSFORM_MAP = {
@ -256,7 +258,6 @@ const TYPE_TRANSFORM_MAP = {
"": "", "": "",
[null]: "", [null]: "",
[undefined]: undefined, [undefined]: undefined,
parse: s => s,
}, },
number: { number: {
"": null, "": null,
@ -268,22 +269,17 @@ const TYPE_TRANSFORM_MAP = {
"": null, "": null,
[undefined]: undefined, [undefined]: undefined,
[null]: null, [null]: null,
parse: d => d,
}, },
attachment: { attachment: {
"": [], "": [],
[null]: [], [null]: [],
[undefined]: undefined, [undefined]: undefined,
parse: a => a,
}, },
boolean: { boolean: {
"": null, "": null,
[null]: null, [null]: null,
[undefined]: undefined, [undefined]: undefined,
parse: b => { true: true,
if (b === "true") return true false: false,
if (b === "false") return false
return b
},
}, },
} }