Better validation function in decrees

pull/1/head
yflory 4 years ago
parent 293e6ae7a2
commit 7f44009617

@ -129,13 +129,19 @@ commands.SET_ACCOUNT_RETENTION_TIME = makeIntegerSetter('accountRetentionTime');
var args_isString = function (args) { var args_isString = function (args) {
return Array.isArray(args) && typeof(args[0]) === "string"; return Array.isArray(args) && typeof(args[0]) === "string";
}; };
// Maintenance: Empty string or an object with a start and end time
var isNumber = function (value) {
return typeof(value) === "number" && !isNaN(value);
};
var args_isMaintenance = function (args) { var args_isMaintenance = function (args) {
return Array.isArray(args) && args[0] && args[0].end && args[0].start; // XXX we could validate that these are numbers && !isNaN return Array.isArray(args) && args[0] &&
(args[0] === "" || (isNumber(args[0].end) && isNumber(args[0].start)));
}; };
var makeBroadcastSetter = function (attr) { // XXX could pass extra validation here? var makeBroadcastSetter = function (attr, validation) {
return function (Env, args) { return function (Env, args) {
if (!args_isString(args) && !args_isMaintenance(args)) { if ((validation && !validation(args)) || !args_isString(args)) {
throw new Error('INVALID_ARGS'); throw new Error('INVALID_ARGS');
} }
var str = args[0]; var str = args[0];
@ -154,7 +160,7 @@ commands.SET_SURVEY_URL = makeBroadcastSetter('surveyURL'); // XXX anticipate la
// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAINTENANCE', [{start: +Date, end: +Date}]]], console.log) // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAINTENANCE', [{start: +Date, end: +Date}]]], console.log)
// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAINTENANCE', [""]]], console.log) // CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAINTENANCE', [""]]], console.log)
commands.SET_MAINTENANCE = makeBroadcastSetter('maintenance'); commands.SET_MAINTENANCE = makeBroadcastSetter('maintenance', args_isMaintenance);
var Quota = require("./commands/quota"); var Quota = require("./commands/quota");
var Keys = require("./keys"); var Keys = require("./keys");

Loading…
Cancel
Save