Update typing of generate-joi

This commit is contained in:
rijkvanzanten
2020-09-09 15:20:51 -04:00
parent eaf36a1b3b
commit 272d0593a9
2 changed files with 57 additions and 20 deletions

View File

@@ -1,10 +1,61 @@
import Joi, { AnySchema } from 'joi';
import BaseJoi, { AnySchema } from 'joi';
/**
* @TODO
* This is copy pasted between app and api. Make this a reusable module.
*/
const Joi = BaseJoi.extend({
type: 'string',
base: BaseJoi.string(),
messages: {
'string.contains': '{{#label}} must contain [{{#substring}}]',
'string.ncontains': "{{#label}} can't contain [{{#substring}}]",
},
rules: {
contains: {
args: [
{
name: 'substring',
ref: true,
assert: (val) => typeof val === 'string',
message: 'must be a string',
},
],
method(substring) {
return this.$_addRule({ name: 'contains', args: { substring } });
},
validate(value, helpers, { substring }, options) {
if (value.includes(substring) === false) {
return helpers.error('string.contains', { substring });
}
return value;
},
},
ncontains: {
args: [
{
name: 'substring',
ref: true,
assert: (val) => typeof val === 'string',
message: 'must be a string',
},
],
method(substring) {
return this.$_addRule({ name: 'ncontains', args: { substring } });
},
validate(value, helpers, { substring }, options) {
if (value.includes(substring) === true) {
return helpers.error('string.ncontains', { substring });
}
return value;
},
},
},
});
type JoiOptions = {
allowUnknown: boolean;
};
@@ -38,27 +89,11 @@ export default function generateJoi(filter: Record<string, any> | null, options?
}
if (operator === '_contains') {
schema[key] = Joi.string().custom((value, helpers) => {
const contains = value.includes(Object.values(value)[0]);
if (contains === false) {
return helpers.error(`"${key}" must include "${Object.values(value)[0]}"`);
}
return value;
});
schema[key] = Joi.string().contains(Object.values(value)[0]);
}
if (operator === '_ncontains') {
schema[key] = Joi.string().custom((value, helpers) => {
const contains = value.includes(Object.values(value)[0]);
if (contains === true) {
return helpers.error(`"${key}" can't include "${Object.values(value)[0]}"`);
}
return value;
});
schema[key] = Joi.string().ncontains(Object.values(value)[0]);
}
if (operator === '_in') {