Commit 5dd2c149 authored by Jure's avatar Jure

Merge branch 'serialisable-config' into 'master'

Serialisable config

See merge request !125
parents a196ea36 c9030b06
Pipeline #2909 canceled with stage
const testMode = require('../test/helpers/authsome_mode')
const Joi = require('joi')
const path = require('path')
const winston = require('winston')
module.exports = {
......@@ -8,31 +7,9 @@ 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: testMode,
mode: path.resolve(__dirname, '..', 'test', 'helpers', 'authsome_mode'),
teams: {
teamContributors: {
name: 'Contributors',
......
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
......@@ -2,7 +2,9 @@
const config = require('config')
const Authsome = require('authsome')
const models = require('../models')
const authsome = new Authsome(config.authsome, {
const mode = require(config.get('authsome.mode'))
const authsome = new Authsome({...config.authsome, mode}, {
// restrict methods passed to mode since these have to be shimmed on client
// any changes here should be reflected in the `withAuthsome` component of `pubsweet-client`
models: {
......
......@@ -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 appValidations = require(config.validations)
const validations = require('./validations')(appValidations)
schema()
......
......@@ -57,26 +57,26 @@ let validations = {
}
}
let allValidations = function (type, config) {
let extraValidations = {}
let allValidations = function (type, appValidations) {
let appValidationsForType = {}
if (config.validations && config.validations[type]) {
extraValidations = config.validations[type]
if (appValidations && appValidations[type]) {
appValidationsForType = appValidations[type]
}
if (Array.isArray(extraValidations)) {
const alternatives = extraValidations.map(extra => ({...validations[type], ...extra}))
if (Array.isArray(appValidationsForType)) {
const alternatives = appValidationsForType.map(alternative => ({...validations[type], ...alternative}))
return Joi.alternatives().try(...alternatives)
}
return Joi.object().keys({...validations[type], ...extraValidations})
return Joi.object().keys({...validations[type], ...appValidationsForType})
}
module.exports = function (config) {
module.exports = function (appValidations) {
return {
fragment: allValidations('fragment', config),
collection: allValidations('collection', config),
user: allValidations('user', config),
team: allValidations('team', config)
fragment: allValidations('fragment', appValidations),
collection: allValidations('collection', appValidations),
user: allValidations('user', appValidations),
team: allValidations('team', appValidations)
}
}
const Fragment = require('../src/models/Fragment')
const validations = require('../src/models/validations')(require('config'))
const config = require('config')
const appValidations = require(config.validations)
const validations = require('../src/models/validations')(appValidations)
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