|
|
|
@ -22,7 +22,6 @@ exports.patch = async function(ctx) { |
|
|
|
if (!model.schema[key]) continue |
|
|
|
record[key] = patchfields[key] |
|
|
|
} |
|
|
|
coerceFieldsToCorrectType(record, model) |
|
|
|
|
|
|
|
const validateResult = await validate({ |
|
|
|
record, |
|
|
|
@ -58,8 +57,6 @@ exports.save = async function(ctx) { |
|
|
|
|
|
|
|
const model = await db.get(record.modelId) |
|
|
|
|
|
|
|
coerceFieldsToCorrectType(record, model) |
|
|
|
|
|
|
|
const validateResult = await validate({ |
|
|
|
record, |
|
|
|
model, |
|
|
|
@ -195,32 +192,6 @@ exports.validate = async function(ctx) { |
|
|
|
ctx.body = errors |
|
|
|
} |
|
|
|
|
|
|
|
// this function modifies an incoming record, to allow for things like
|
|
|
|
// "boolField": "true" (instead of mandating "boolField": true)
|
|
|
|
// this allows us to use mustash templating to send non-string fields in a request
|
|
|
|
const coerceFieldsToCorrectType = (record, model) => { |
|
|
|
for (let fieldName in record) { |
|
|
|
const fieldValue = record[fieldName] |
|
|
|
if (model.schema[fieldName]) { |
|
|
|
if ( |
|
|
|
model.schema[fieldName].type === "boolean" && |
|
|
|
typeof fieldValue !== "boolean" |
|
|
|
) { |
|
|
|
if (fieldValue === "true") record[fieldName] = true |
|
|
|
if (fieldValue === "false") record[fieldName] = false |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
if (model.schema[fieldName].type === "number") { |
|
|
|
const val = parseFloat(fieldValue) |
|
|
|
if (!isNaN(val)) { |
|
|
|
record[fieldName] = val |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function validate({ instanceId, modelId, record, model }) { |
|
|
|
if (!model) { |
|
|
|
const db = new CouchDB(instanceId) |
|
|
|
|