From 0ffc960523b9f1855fc69b70be2eda3bd7bb69a0 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 19 May 2021 01:52:37 -0400 Subject: [PATCH] add comments --- server/config.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/server/config.js b/server/config.js index 0cea3996..833ddf10 100644 --- a/server/config.js +++ b/server/config.js @@ -5,16 +5,19 @@ const { randomBytes } = require('crypto'); convict.addFormat({ name: 'positive-int-array', - validate: (ints, schema) => { + coerce: (ints, schema) => { // can take: int[] | string[] | string (csv), returns -> int[] + const ints_arr = Array.isArray(ints) ? ints : ints.trim().split(',') + return ints_arr.map(int => + (typeof int === 'number') + ? int + : parseInt(int.trim(), 10)) + }, + validate: (ints, schema) => { // takes: int[], errors if any NaNs, negatives, or floats present for (const int of ints) { - if (isNaN(int) || int < 0) + if (typeof(int) !== 'number' || isNaN(int) || int < 0 || int % 1 > 0) throw new Error('must be a comma-separated list of positive integers') } }, - coerce: (ints, schema) => { - const ints_arr = Array.isArray(ints_str) ? ints : ints.trim().split(',') - return ints_arr.map(int => (typeof int === 'number') ? int : parseInt(int.trim(), 10)) - }, }); const conf = convict({