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

Commit

Permalink
feat(stringOrTranslations): add back length limit
Browse files Browse the repository at this point in the history
BREAKING CHANGE localised entities names are now limited in length
  • Loading branch information
Arinono committed Oct 27, 2022
1 parent fd789b7 commit eeb37c1
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as commons from './commons';

import { Response as MachineLoginResponse } from './routes/authentication/machine-login';

const apiVersion = 8;
const apiVersion = 9;

class PlatformSdk {
protected comms: Comms;
Expand Down
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
24 changes: 12 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,12 @@ 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\', \'file\' and \'files\''),
suffix: stringOrTranslationsSchema.description('Not available for inputTypes \'radio\', \'switch\', \'checkbox\', \'file\' and \'files\''),
prefix: stringOrTranslationsSchema().description('Not available for inputTypes \'radio\', \'switch\', \'checkbox\', \'file\' and \'files\''),
suffix: stringOrTranslationsSchema().description('Not available for inputTypes \'radio\', \'switch\', \'checkbox\', \'file\' and \'files\''),
};

const schema = (apiVersion: number): Joi.AlternativesSchema => {
Expand Down Expand Up @@ -68,7 +68,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 +78,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 +109,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 +120,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 +130,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 +140,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 +158,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 +168,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
4 changes: 2 additions & 2 deletions src/models/quantity.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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
Expand Down
26 changes: 18 additions & 8 deletions src/models/string-or-translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ 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,
const schema = (limit = 9999): Joi.AnySchema => Joi.alternatives().try(
Joi.string().example('untranslated string').meta({ className: 'untranslatedString' }).max(limit),
translationsSchema(limit),
)
.tag('stringOrTranslations')
.meta({ className: 'stringOrTranslations' });

const stringBeforeV7ElseStringOrTranslationSchema = (apiVersion: number): Joi.AnySchema => {
if (apiVersion <= 6) {
return Joi.string();
const versionedStringOrStringOrTranslationSchema = (apiVersion: number): Joi.AnySchema => {
// [min, max, schema]
// range being [x;y)
const ranges: [number, number, Joi.AnySchema][] = [
[9, 9999, schema(255)],
[8, 9, schema()],
[0, 7, Joi.string()],
];
for (let i = 0, len = ranges.length; i < len; i += 1) {
const [min, max, appliedSchema] = ranges[i];
if (apiVersion >= min && apiVersion < max) {
return appliedSchema;
}
}
return schema;
return schema(255);
};

type StringOrTranslations = string | Translations;
Expand All @@ -39,5 +49,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
14 changes: 7 additions & 7 deletions src/models/translations.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Joi from 'joi';

const translationStringSchema = Joi.string().allow('').required();
const translationSchema = (example: string) => Joi.alternatives().try(
const translationSchema = (example: string, limit = 9999) => Joi.alternatives().try(
Joi.object({
plural: translationStringSchema,
singular: translationStringSchema,
plural: translationStringSchema.max(limit),
singular: translationStringSchema.max(limit),
}),
translationStringSchema.example(example),
translationStringSchema.example(example).max(limit),
);
const schema = Joi.object().keys({
en: translationSchema('English string'),
nl: translationSchema('Nederlandse string'),
const schema = (limit = 9999): Joi.AnySchema => Joi.object().keys({
en: translationSchema('English string', limit),
nl: translationSchema('Nederlandse string', limit),
})
.tag('translations')
.meta({ className: 'translations' });
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().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().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
10 changes: 5 additions & 5 deletions src/routes/device-type/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const controllerGeneratorOptions: ControllerGeneratorOptionsWithSupplier = {
method: 'post',
path: '/',
body: (apiVersion: number): Joi.ObjectSchema => Joi.object().keys({
name: stringOrTranslationsSchema.required().example('Cathodic protection device').description('This name is also visible in monitoring environments. To get a uniform user experience, please provide the name in English'),
name: stringOrTranslationsSchema().required().example('Cathodic protection device').description('This name is also visible in monitoring environments. To get a uniform user experience, please provide the name in English'),
eventHandler: Joi.string().max(1000000).required().example('[omitted]')
.description('A javascript function that handles events. See the chapter "User defined code'),
fieldConfigurations: Joi.array().items(baseFieldConfigurationSchema(apiVersion)).required()
Expand All @@ -53,12 +53,12 @@ const controllerGeneratorOptions: ControllerGeneratorOptionsWithSupplier = {
.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: stringOrTranslationsSchema.required().example('Red wire').description('This name is also visible in monitoring environments. To get a uniform user experience, please provide the name in English'),
name: stringOrTranslationsSchema().required().example('Red wire').description('This name is also visible in monitoring environments. To get a uniform user experience, please provide the name in English'),
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: stringOrTranslationsSchema.example('Anode').description('If undefined, the channel cannot be linked to a pin'),
defaultPinName: stringOrTranslationsSchema().example('Anode').description('If undefined, the channel cannot be linked to a pin'),
charts: Joi.array().items(Joi.object().keys({
title: stringOrTranslationsSchema.allow(null).example('Red wire charts').required(),
title: stringOrTranslationsSchema().allow(null).example('Red wire charts').required(),
series: Joi.array().items(Joi.object().keys({
quantityHashId: Joi.string().example('x18a92').required(),
color: Joi.string().example('#ff00ff').allow(null)
Expand All @@ -67,7 +67,7 @@ const controllerGeneratorOptions: ControllerGeneratorOptionsWithSupplier = {
})),
})).required().description('All measurements are registered on a channel. When a device is installed at a location (pinGroup), its channels are connected to the ports (pins) of the location(pinGroup).'),
charts: Joi.array().items(Joi.object().keys({
title: stringOrTranslationsSchema.allow(null).example('Cathodic protection charts').required(),
title: stringOrTranslationsSchema().allow(null).example('Cathodic protection charts').required(),
series: Joi.array().items(Joi.object().keys({
channelIndex: Joi.number().integer().required(),
quantityHashId: Joi.string().example('x18a92').required(),
Expand Down
Loading

0 comments on commit eeb37c1

Please sign in to comment.