Commit 2ff3ae9b authored by Tamlyn Rhodes's avatar Tamlyn Rhodes

Use new app validations config

parent 9e24c69a
Pipeline #2894 passed with stage
in 4 minutes and 46 seconds
const path = require('path')
const Joi = require('joi')
const winston = require('winston')
module.exports = {
......@@ -8,29 +7,7 @@ module.exports = {
secret: 'test',
sse: false
},
validations: {
fragment: [
{
fragmentType: Joi.valid('blogpost').required(),
source: Joi.string(),
kind: Joi.string(),
presentation: Joi.string(),
published: Joi.boolean(),
filtered: Joi.string()
},
{
fragmentType: Joi.valid('file').required(),
path: Joi.string().required()
}
],
collection: {
published: Joi.boolean(),
nonPublicProperty: Joi.string(),
filtered: Joi.string(),
created: Joi.date().default(Date.now, 'creation time'),
title: Joi.string()
}
},
validations: path.join(__dirname, 'validations'),
authsome: {
mode: path.resolve(__dirname, '..', 'test', 'helpers', 'authsome_mode'),
teams: {
......
const Joi = require('joi')
module.exports = {
fragment: [
{
fragmentType: Joi.valid('blogpost').required(),
source: Joi.string(),
kind: Joi.string(),
presentation: Joi.string(),
published: Joi.boolean(),
filtered: Joi.string()
},
{
fragmentType: Joi.valid('file').required(),
path: Joi.string().required()
}
],
collection: {
published: Joi.boolean(),
nonPublicProperty: Joi.string(),
filtered: Joi.string(),
created: Joi.date().default(Date.now, 'creation time'),
title: Joi.string()
}
}
\ No newline at end of file
......@@ -8,7 +8,10 @@ const schema = require('./schema')
const NotFoundError = require('../errors/NotFoundError')
const ValidationError = require('../errors/ValidationError')
const logger = require('@pubsweet/logger')
const validations = require('./validations')(require('config'))
const config = require('config')
const extraValidations = require(config.validations)
const validations = require('./validations')(extraValidations)
schema()
......
......@@ -57,26 +57,26 @@ let validations = {
}
}
let allValidations = function (type, config) {
let extraValidations = {}
let allValidations = function (type, extraValidations) {
let extraValidationsForType = {}
if (config.validations && config.validations[type]) {
extraValidations = config.validations[type]
if (extraValidations && extraValidations[type]) {
extraValidationsForType = extraValidations[type]
}
if (Array.isArray(extraValidations)) {
const alternatives = extraValidations.map(extra => ({...validations[type], ...extra}))
if (Array.isArray(extraValidationsForType)) {
const alternatives = extraValidationsForType.map(extra => ({...validations[type], ...extra}))
return Joi.alternatives().try(...alternatives)
}
return Joi.object().keys({...validations[type], ...extraValidations})
return Joi.object().keys({...validations[type], ...extraValidationsForType})
}
module.exports = function (config) {
module.exports = function (extraValidations) {
return {
fragment: allValidations('fragment', config),
collection: allValidations('collection', config),
user: allValidations('user', config),
team: allValidations('team', config)
fragment: allValidations('fragment', extraValidations),
collection: allValidations('collection', extraValidations),
user: allValidations('user', extraValidations),
team: allValidations('team', extraValidations)
}
}
const Fragment = require('../src/models/Fragment')
const validations = require('../src/models/validations')(require('config'))
const config = require('config')
const extraValidations = require(config.validations)
const validations = require('../src/models/validations')(extraValidations)
describe('Validations export', function () {
it('has validations for each type', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment