Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
fix(quantity): add several name length limits (#356)
Browse files Browse the repository at this point in the history
* fix(quantity): add unit name length limit

* feat(stringOrTranslations): add back length limit

* fix(measurement-filter): add name and desc input limit

* fix(webhook): add name len limit

* fix(environment): add name len limit

* rebase fix

* chore(docs): update

* fix: remove unwanted breaking change

* chore: Documentation updated by github actions

* chore: remove abitrary max apiVersion number

* fix(measurement-filter): add missing limit on model

* chore: Documentation updated by github actions

* refactor(string-or-translation): simplify versionning

* address feedback

* chore: Documentation updated by github actions

BREAKING CHANGE: localised entities names are now limited in length
  • Loading branch information
Arinono authored Jan 30, 2023
1 parent 1f81947 commit 4eb8bd5
Show file tree
Hide file tree
Showing 25 changed files with 209 additions and 83 deletions.
18 changes: 9 additions & 9 deletions docs/index.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/models/command-type.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Joi from 'joi';

import { schema as baseFieldConfigurationSchema, BaseFieldConfiguration } from './fields/base-field-configuration';
import { stringBeforeV7ElseStringOrTranslationSchema, StringOrTranslations } from './string-or-translations';
import { versionedStringOrStringOrTranslationSchema, StringOrTranslations } from './string-or-translations';

const schema = (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
hashId: Joi.string().required().example('x18a92'),
name: stringBeforeV7ElseStringOrTranslationSchema(apiVersion).required().example('Measurement cycle'),
name: versionedStringOrStringOrTranslationSchema(apiVersion).required().example('Measurement cycle'),
start: Joi.string().valid('required', 'optional', 'disabled').required().example('required')
.description('\'required\': user must provide command.startAt. \'optional\': user can provide command.startAt or a delay for the command to start after it is sent to the device. \'disabled\': user cannot provide command.startAt nor a delay.'),
end: Joi.string().valid('required', 'optional', 'disabled').required().example('disabled')
Expand Down
12 changes: 6 additions & 6 deletions src/models/device-type.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import Joi from 'joi';
import { schema as baseFieldConfigurationSchema, BaseFieldConfiguration } from './fields/base-field-configuration';
import {
stringBeforeV7ElseStringOrTranslationSchema,
versionedStringOrStringOrTranslationSchema,
StringOrTranslations,
} from './string-or-translations';

const schema = (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
hashId: Joi.string().required().example('wasd2'),
name: stringBeforeV7ElseStringOrTranslationSchema(apiVersion).required().example('Cathodic protection device'),
name: versionedStringOrStringOrTranslationSchema(apiVersion).required().example('Cathodic protection device'),
fieldConfigurations: Joi.array().items(baseFieldConfigurationSchema(apiVersion)).required()
.description('See the chapter on open fields on how to use this'),
pinGroupFieldConfigurations: Joi.array()
.items(baseFieldConfigurationSchema(apiVersion))
.required()
.description('Defines deviceFields on the location (pinGroup) the device is connected to. Can be used in report type functions. See the chapter on open fields on how to use this'),
channels: Joi.array().items(Joi.object().keys({
name: stringBeforeV7ElseStringOrTranslationSchema(apiVersion).required().example('Red wire'),
name: versionedStringOrStringOrTranslationSchema(apiVersion).required().example('Red wire'),
pinFieldConfigurations: Joi.array().items(baseFieldConfigurationSchema(apiVersion)).required()
.description('Defines deviceFields on the pin the channel is connected to. Can be used in report type functions. See the chapter on open fields on how to use this'),
defaultPinName: stringBeforeV7ElseStringOrTranslationSchema(apiVersion).example('Anode').description('If undefined, the channel cannot be linked to a pin'),
defaultPinName: versionedStringOrStringOrTranslationSchema(apiVersion).example('Anode').description('If undefined, the channel cannot be linked to a pin'),
charts: Joi.array().items(Joi.object().keys({
title: stringBeforeV7ElseStringOrTranslationSchema(apiVersion).required().allow(null).example('Red wire charts'),
title: versionedStringOrStringOrTranslationSchema(apiVersion).required().allow(null).example('Red wire charts'),
series: Joi.array().items(Joi.object().keys({
quantityHashId: Joi.string().example('x18a92').required(),
color: Joi.string().example('#ff00ff').allow(null)
Expand All @@ -29,7 +29,7 @@ const schema = (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
})).required(),
})).required(),
charts: Joi.array().items(Joi.object().keys({
title: stringBeforeV7ElseStringOrTranslationSchema(apiVersion).required().allow(null).example('Cathodic protection charts'),
title: versionedStringOrStringOrTranslationSchema(apiVersion).required().allow(null).example('Cathodic protection charts'),
series: Joi.array().items(Joi.object().keys({
channelIndex: Joi.number().integer().example(0).required(),
quantityHashId: Joi.string().example('x18a92').required(),
Expand Down
2 changes: 1 addition & 1 deletion src/models/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { themeSchema, Theme } from '../commons/theme';

const schema = (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
hashId: Joi.string().required().example('f1a4w1'),
name: Joi.string().required().example('My monitoring environment'),
name: Joi.string().required().example('My monitoring environment').max(255),
mapLayers: Joi.array().items(mapLayerSchema).min(1).required(),
boundingBox: Joi.object().keys({
type: Joi.string().valid('LineString').required().example('LineString'),
Expand Down
26 changes: 14 additions & 12 deletions src/models/fields/base-field-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

const commonBaseFieldConfigurationSchema = Joi.object().keys({
key: Joi.string().pattern(/^[a-z][a-zA-Z\d]*$/).required().example('id'),
name: stringOrTranslationsSchema.required(),
name: stringOrTranslationsSchema().required(),
showIf: Joi.object().keys({
key: Joi.string().required(),
value: Joi.alternatives().try(
Expand All @@ -16,12 +16,14 @@ const commonBaseFieldConfigurationSchema = Joi.object().keys({
Joi.boolean().strict(),
).required(),
}).description('Show this field if other field with provided key is set to provided value. Referenced field must exists already'),
hint: stringOrTranslationsSchema.allow('').description('As shown near the input field'),
hint: stringOrTranslationsSchema().allow('').description('As shown near the input field'),
});

const prefixesMixin = {
prefix: stringOrTranslationsSchema.description('Not available for inputTypes \'radio\', \'switch\', \'checkbox\', \'pinHashId\', \'pinGroupHashId\', \'file\' and \'files\''),
suffix: stringOrTranslationsSchema.description('Not available for inputTypes \'radio\', \'switch\', \'checkbox\', \'pinHashId\', \'pinGroupHashId\', \'file\' and \'files\''),
prefix: stringOrTranslationsSchema()
.description('Not available for inputTypes \'radio\', \'switch\', \'checkbox\', \'pinHashId\', \'pinGroupHashId\', \'file\' and \'files\''),
suffix: stringOrTranslationsSchema()
.description('Not available for inputTypes \'radio\', \'switch\', \'checkbox\', \'pinHashId\', \'pinGroupHashId\', \'file\' and \'files\''),
};

const schema = (apiVersion: number): Joi.AlternativesSchema => {
Expand Down Expand Up @@ -68,7 +70,7 @@ const schema = (apiVersion: number): Joi.AlternativesSchema => {
type: Joi.string().valid('string').default('string'),
defaultValue: Joi.string().allow(''),
valueOptions: Joi.array().min(1).items(Joi.object().keys({
text: stringOrTranslationsSchema.required(),
text: stringOrTranslationsSchema().required(),
value: Joi.string().allow('').required(),
})).required(),
inputType: Joi.string().valid('select').default('select'),
Expand All @@ -78,7 +80,7 @@ const schema = (apiVersion: number): Joi.AlternativesSchema => {
type: Joi.string().valid('string').default('string'),
...deprecatedDefaultValue,
valueOptions: Joi.array().min(1).items(Joi.object().keys({
text: stringOrTranslationsSchema.required(),
text: stringOrTranslationsSchema().required(),
value: Joi.string().allow('').required(),
})).required(),
inputType: Joi.string().valid('radio').required(),
Expand Down Expand Up @@ -109,7 +111,7 @@ const schema = (apiVersion: number): Joi.AlternativesSchema => {
type: Joi.string().valid('number').default('number'),
defaultValue: Joi.number(),
valueOptions: Joi.array().min(1).items(Joi.object().keys({
text: stringOrTranslationsSchema.required(),
text: stringOrTranslationsSchema().required(),
value: Joi.number().required(),
})).required(),
inputType: Joi.string().valid('select').default('select'),
Expand All @@ -120,7 +122,7 @@ const schema = (apiVersion: number): Joi.AlternativesSchema => {
type: Joi.string().valid('integer').required(),
defaultValue: Joi.number().integer(),
valueOptions: Joi.array().min(1).items(Joi.object().keys({
text: stringOrTranslationsSchema.required(),
text: stringOrTranslationsSchema().required(),
value: Joi.number().integer().required(),
})).required(),
inputType: Joi.string().valid('select').default('select'),
Expand All @@ -130,7 +132,7 @@ const schema = (apiVersion: number): Joi.AlternativesSchema => {
type: Joi.string().valid('number').default('number'),
...deprecatedDefaultValue,
valueOptions: Joi.array().min(1).items(Joi.object().keys({
text: stringOrTranslationsSchema.required(),
text: stringOrTranslationsSchema().required(),
value: Joi.number().required(),
})).required(),
inputType: Joi.string().valid('radio').required(),
Expand All @@ -140,7 +142,7 @@ const schema = (apiVersion: number): Joi.AlternativesSchema => {
type: Joi.string().valid('integer').required(),
...deprecatedDefaultValue,
valueOptions: Joi.array().min(1).items(Joi.object().keys({
text: stringOrTranslationsSchema.required(),
text: stringOrTranslationsSchema().required(),
value: Joi.number().integer().required(),
})).required(),
inputType: Joi.string().valid('radio').required(),
Expand All @@ -158,7 +160,7 @@ const schema = (apiVersion: number): Joi.AlternativesSchema => {
type: Joi.string().valid('boolean').default('boolean'),
defaultValue: Joi.boolean(),
valueOptions: Joi.array().min(1).items(Joi.object().keys({
text: stringOrTranslationsSchema.required(),
text: stringOrTranslationsSchema().required(),
value: Joi.boolean().required(),
})).required(),
inputType: Joi.string().valid('select').default('select'),
Expand All @@ -168,7 +170,7 @@ const schema = (apiVersion: number): Joi.AlternativesSchema => {
type: Joi.string().valid('boolean').default('boolean'),
...deprecatedDefaultValue,
valueOptions: Joi.array().min(1).items(Joi.object().keys({
text: stringOrTranslationsSchema.required(),
text: stringOrTranslationsSchema().required(),
value: Joi.boolean().required(),
})).required(),
inputType: Joi.string().valid('radio').required(),
Expand Down
9 changes: 7 additions & 2 deletions src/models/measurement-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import Joi from 'joi';

const schema = Joi.object().keys({
hashId: Joi.string().required().example('k8gh3'),
name: Joi.string().required().example('North'),
description: Joi.string().required().allow('').example('Temperatures in the North'),
name: Joi.string().required().example('North').max(100),
description: Joi
.string()
.required()
.allow('')
.example('Temperatures in the North')
.max(255),
period: Joi.alternatives().try(
Joi.string().valid('lastMonth', 'lastQuarter', 'lastYear').required().example('lastMonth'),
Joi.object().keys({
Expand Down
5 changes: 3 additions & 2 deletions src/models/quantity.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import Joi from 'joi';
import { schema as siNumberSchema, SiNumber } from './si-number';
import {
stringBeforeV7ElseStringOrTranslationSchema,
versionedStringOrStringOrTranslationSchema,
StringOrTranslations,
} from './string-or-translations';

const schema = (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
hashId: Joi.string().required().example('sajia1'),
name: stringBeforeV7ElseStringOrTranslationSchema(apiVersion).required().example('Temperature'),
name: versionedStringOrStringOrTranslationSchema(apiVersion).required().example('Temperature'),
color: Joi.string().required().example('#ff00ff'),
unit: (
apiVersion >= 8
? Joi.string().allow(null).default(null)
: Joi.string().required()
).example('K')
.max(10)
.description('Will be displayed with an SI-prefix (eg. k or M) if relevant'),
defaultOrderOfMagnitude: Joi.number().integer().min(-128).max(127)
.default(0)
Expand Down
29 changes: 20 additions & 9 deletions src/models/string-or-translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ import Joi from 'joi';
import { schema as translationsSchema, Translations } from './translations';
import { Locale } from './locale';

const schema = Joi.alternatives().try(
Joi.string().example('untranslated string').meta({ className: 'untranslatedString' }),
translationsSchema,
)
.tag('stringOrTranslations')
.meta({ className: 'stringOrTranslations' });
const schema = (limit?: number): Joi.AnySchema => {
if (limit !== undefined) {
return Joi.alternatives().try(
Joi.string().example('untranslated string').meta({ className: 'untranslatedString' }).max(limit),
translationsSchema(limit),
)
.tag('stringOrTranslations')
.meta({ className: 'stringOrTranslations' });
}

return Joi.alternatives().try(
Joi.string().example('untranslated string').meta({ className: 'untranslatedString' }),
translationsSchema(),
)
.tag('stringOrTranslations')
.meta({ className: 'stringOrTranslations' });
};

const stringBeforeV7ElseStringOrTranslationSchema = (apiVersion: number): Joi.AnySchema => {
const versionedStringOrStringOrTranslationSchema = (apiVersion: number): Joi.AnySchema => {
if (apiVersion <= 6) {
return Joi.string();
}
return schema;
return schema(255);
};

type StringOrTranslations = string | Translations;
Expand All @@ -39,5 +50,5 @@ export {
schema,
StringOrTranslations,
getTranslatedString,
stringBeforeV7ElseStringOrTranslationSchema,
versionedStringOrStringOrTranslationSchema,
};
4 changes: 2 additions & 2 deletions src/models/supplier-report-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import Joi from 'joi';

import { schema as baseFieldConfigurationSchema, BaseFieldConfiguration } from './fields/base-field-configuration';
import {
stringBeforeV7ElseStringOrTranslationSchema,
versionedStringOrStringOrTranslationSchema,
StringOrTranslations,
} from './string-or-translations';

const schema = (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
hashId: Joi.string().required().example('l19a7s'),
name: stringBeforeV7ElseStringOrTranslationSchema(apiVersion).required().example('Temperature and inclination'),
name: versionedStringOrStringOrTranslationSchema(apiVersion).required().example('Temperature and inclination'),
fieldConfigurations: Joi.object().keys({
pinGroup: Joi.array().items(baseFieldConfigurationSchema(apiVersion)).required(),
pin: Joi.array().items(baseFieldConfigurationSchema(apiVersion)).required(),
Expand Down
2 changes: 1 addition & 1 deletion src/models/supplier-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const identifierExample = `function (command) {

const schema = Joi.object().keys({
hashId: Joi.string().required().example('z812a63'),
name: Joi.string().required().example('My webhook'),
name: Joi.string().required().example('My webhook').max(255),
createdAt: Joi.date().required().example('2019-12-31T15:23Z'),
})
.tag('supplierWebhook')
Expand Down
50 changes: 37 additions & 13 deletions src/models/translations.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
import Joi from 'joi';

const translationStringSchema = Joi.string().allow('').required();
const translationSchema = (example: string) => Joi.alternatives().try(
Joi.object({
plural: translationStringSchema,
singular: translationStringSchema,
}),
translationStringSchema.example(example),
);
const schema = Joi.object().keys({
en: translationSchema('English string'),
nl: translationSchema('Nederlandse string'),
})
.tag('translations')
.meta({ className: 'translations' });
const translationSchema = (example: string, limit?: number): Joi.AnySchema => {
if (limit !== undefined) {
return Joi.alternatives().try(
Joi.object({
plural: translationStringSchema.max(limit),
singular: translationStringSchema.max(limit),
}),
translationStringSchema.example(example).max(limit),
);
}

return Joi.alternatives().try(
Joi.object({
plural: translationStringSchema,
singular: translationStringSchema,
}),
translationStringSchema.example(example),
);
};

const schema = (limit?: number): Joi.AnySchema => {
if (limit !== undefined) {
return Joi.object().keys({
en: translationSchema('English string', limit),
nl: translationSchema('Nederlandse string', limit),
})
.tag('translations')
.meta({ className: 'translations' });
}

return Joi.object().keys({
en: translationSchema('English string'),
nl: translationSchema('Nederlandse string'),
})
.tag('translations')
.meta({ className: 'translations' });
};

type SimpleTranslation = string
type PluralizedTranslation = Record<'plural' | 'singular', string>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/command-type/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const controllerGeneratorOptions: ControllerGeneratorOptionsWithSupplier = {
method: 'post',
path: '/',
body: (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
name: stringOrTranslationsSchema.required().example('Measurement cycle'),
name: stringOrTranslationsSchema(255).required().example('Measurement cycle'),
start: Joi.string().valid('required', 'optional', 'disabled').default('optional').description('\'required\': user must provide command.startAt. \'optional\': user can provide command.startAt or a delay for the command to start after it is sent to the device. \'disabled\': user cannot provide command.startAt nor a delay.'),
end: Joi.string().valid('required', 'optional', 'disabled').default('disabled').description('\'required\': user must provide command.endAt. \'optional\': user can provide command.endAt. \'disabled\': user cannot provide command.endAt.'),
fieldConfigurations: Joi.array().items(baseFieldConfigurationSchema(apiVersion)).required()
Expand Down
2 changes: 1 addition & 1 deletion src/routes/command-type/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const controllerGeneratorOptions: ControllerGeneratorOptionsWithSupplier = {
hashId: Joi.string().required().example('x18a92'),
}).required(),
body: (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
name: stringOrTranslationsSchema.example('Measurement cycle'),
name: stringOrTranslationsSchema(255).example('Measurement cycle'),
start: Joi.string().valid('required', 'optional', 'disabled').description('\'required\': user must provide command.startAt. \'optional\': user can provide command.startAt or a delay for the command to start after it is sent to the device. \'disabled\': user cannot provide command.startAt nor a delay.'),
end: Joi.string().valid('required', 'optional', 'disabled').description('\'required\': user must provide command.endAt. \'optional\': user can provide command.endAt. \'disabled\': user cannot provide command.endAt.'),
fieldConfigurations: updatableFieldConfigurationsSchema(apiVersion).description('See the chapter on open fields on how to use this'),
Expand Down
Loading

0 comments on commit 4eb8bd5

Please sign in to comment.