@autofleet/sadot 0.6.4 → 0.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/v1/definition/validations.js +21 -4
- package/dist/hooks/create.d.ts +1 -1
- package/dist/hooks/enrich.d.ts +1 -1
- package/dist/hooks/update.d.ts +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.js +2 -4
- package/dist/models/CustomFieldDefinition.d.ts +1 -1
- package/dist/models/CustomFieldDefinition.js +11 -6
- package/dist/models/CustomFieldValue.js +3 -8
- package/dist/repository/definition.d.ts +2 -2
- package/dist/repository/value.d.ts +2 -2
- package/dist/scopes/filter.d.ts +1 -1
- package/dist/tests/mocks/definition.mock.d.ts +3 -3
- package/dist/tests/mocks/definition.mock.js +8 -10
- package/dist/types/index.d.ts +2 -2
- package/dist/utils/constants/index.d.ts +13 -0
- package/dist/utils/constants/index.js +15 -2
- package/dist/utils/helpers/index.d.ts +3 -3
- package/dist/utils/helpers/index.js +3 -3
- package/dist/utils/validations/index.d.ts +2 -2
- package/dist/utils/validations/index.js +15 -15
- package/dist/utils/validations/{custom-fields.js → schema/custom-fields.js} +0 -1
- package/dist/utils/validations/type.d.ts +10 -14
- package/dist/utils/validations/type.js +0 -48
- package/dist/utils/validations/validators/index.d.ts +14 -0
- package/dist/utils/validations/validators/index.js +33 -0
- package/dist/utils/validations/validators/select.validator.d.ts +5 -0
- package/dist/utils/validations/validators/select.validator.js +9 -0
- package/dist/utils/validations/validators/status.validator.d.ts +12 -0
- package/dist/utils/validations/validators/status.validator.js +9 -0
- package/package.json +9 -8
- package/src/api/v1/definition/index.ts +1 -1
- package/src/api/v1/definition/validations.ts +20 -1
- package/src/hooks/create.ts +1 -1
- package/src/hooks/enrich.ts +4 -4
- package/src/hooks/update.ts +1 -1
- package/src/index.ts +2 -3
- package/src/models/CustomFieldDefinition.ts +7 -5
- package/src/models/CustomFieldValue.ts +1 -5
- package/src/repository/definition.ts +2 -5
- package/src/repository/value.ts +2 -2
- package/src/scopes/filter.ts +1 -1
- package/src/tests/functional/searching/index.ts +1 -1
- package/src/tests/mocks/definition.mock.ts +6 -8
- package/src/types/index.ts +2 -2
- package/src/utils/constants/index.ts +14 -1
- package/src/utils/helpers/index.ts +5 -5
- package/src/utils/init.ts +1 -1
- package/src/utils/validations/index.ts +18 -15
- package/src/utils/validations/{custom-fields.ts → schema/custom-fields.ts} +0 -1
- package/src/utils/validations/type.ts +14 -40
- package/src/utils/validations/validators/index.ts +31 -0
- package/src/utils/validations/validators/select.validator.ts +11 -0
- package/src/utils/validations/validators/status.validator.ts +18 -0
- package/dist/utils/validations/custom.d.ts +0 -15
- package/dist/utils/validations/custom.js +0 -42
- package/dist/utils/validations/validators.d.ts +0 -12
- package/dist/utils/validations/validators.js +0 -33
- package/src/utils/validations/custom.ts +0 -39
- package/src/utils/validations/validators.ts +0 -34
- /package/dist/utils/validations/{custom-fields.d.ts → schema/custom-fields.d.ts} +0 -0
|
@@ -3,7 +3,7 @@ import { Router } from 'express';
|
|
|
3
3
|
import type { Request, Response } from 'express';
|
|
4
4
|
import handleError from '../errors';
|
|
5
5
|
import * as DefinitionRepo from '../../../repository/definition';
|
|
6
|
-
import { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../../../types/definition';
|
|
6
|
+
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../../../types/definition';
|
|
7
7
|
import { validateCustomFieldDefinitionCreation, validateCustomFieldDefinitionUpdate } from './validations';
|
|
8
8
|
import logger from '../../../utils/logger';
|
|
9
9
|
|
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
import Joi from 'joi';
|
|
2
|
-
import { CustomFieldDefinitionType } from '../../../utils/
|
|
2
|
+
import { CustomFieldDefinitionType } from '../../../utils/constants';
|
|
3
3
|
|
|
4
|
+
const statusValidationObjectSchema = Joi.array().items(
|
|
5
|
+
Joi.object({
|
|
6
|
+
value: Joi.string().required(),
|
|
7
|
+
color: Joi.string().required(),
|
|
8
|
+
}),
|
|
9
|
+
).min(1).unique('value');
|
|
10
|
+
/**
|
|
11
|
+
* Schema for the validation of custom field definition
|
|
12
|
+
* The only custom validation is for
|
|
13
|
+
* {@link CustomFieldDefinitionType.SELECT SELECT}
|
|
14
|
+
* and
|
|
15
|
+
* {@link CustomFieldDefinitionType.STATUS STATUS}
|
|
16
|
+
* field types.
|
|
17
|
+
* The rest of the field types are validated by Joi
|
|
18
|
+
*/
|
|
4
19
|
const ValidationSchema = Joi.when('fieldType', {
|
|
5
20
|
is: CustomFieldDefinitionType.SELECT,
|
|
6
21
|
then: Joi.array().items(Joi.string()).min(1).unique(),
|
|
7
22
|
otherwise: Joi.any(),
|
|
23
|
+
}).when('fieldType', {
|
|
24
|
+
is: CustomFieldDefinitionType.STATUS,
|
|
25
|
+
then: statusValidationObjectSchema,
|
|
26
|
+
otherwise: Joi.any(),
|
|
8
27
|
});
|
|
9
28
|
|
|
10
29
|
const CustomFieldDefinitionCreationSchema = Joi.object({
|
package/src/hooks/create.ts
CHANGED
|
@@ -2,7 +2,7 @@ import logger from '../utils/logger';
|
|
|
2
2
|
import * as ValueRepo from '../repository/value';
|
|
3
3
|
import * as DefinitionRepo from '../repository/definition';
|
|
4
4
|
import { MissingRequiredCustomFieldError } from '../errors';
|
|
5
|
-
import { ModelOptions } from '../types';
|
|
5
|
+
import type { ModelOptions } from '../types';
|
|
6
6
|
import applyScopeToInstance from '../utils/scopeAttributes';
|
|
7
7
|
|
|
8
8
|
/**
|
package/src/hooks/enrich.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/* eslint-disable no-param-reassign */
|
|
2
2
|
import * as ValueRepo from '../repository/value';
|
|
3
3
|
import * as DefinitionRepo from '../repository/definition';
|
|
4
|
-
import CustomFieldValue from '../models/CustomFieldValue';
|
|
5
|
-
import CustomFieldDefinition from '../models/CustomFieldDefinition';
|
|
6
|
-
import { SerializedCustomFields } from '../types/definition';
|
|
7
|
-
import { ModelOptions } from '../types';
|
|
4
|
+
import type CustomFieldValue from '../models/CustomFieldValue';
|
|
5
|
+
import type CustomFieldDefinition from '../models/CustomFieldDefinition';
|
|
6
|
+
import type { SerializedCustomFields } from '../types/definition';
|
|
7
|
+
import type { ModelOptions } from '../types';
|
|
8
8
|
import applyScopeToInstance from '../utils/scopeAttributes';
|
|
9
9
|
|
|
10
10
|
type SupportedHookTypes = 'afterFind' | 'afterCreate' | 'afterUpdate';
|
package/src/hooks/update.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Application } from 'express';
|
|
2
|
-
import { Sequelize } from 'sequelize-typescript';
|
|
2
|
+
import type { Sequelize } from 'sequelize-typescript';
|
|
3
3
|
import {
|
|
4
4
|
initTables, initTestModels,
|
|
5
5
|
} from './models';
|
|
@@ -11,13 +11,12 @@ import {
|
|
|
11
11
|
addHooks, addScopes, applyCustomAssociation, removeHooks,
|
|
12
12
|
} from './utils/init';
|
|
13
13
|
|
|
14
|
-
export * from './utils/validations/custom-fields';
|
|
14
|
+
export * from './utils/validations/schema/custom-fields';
|
|
15
15
|
|
|
16
16
|
export * from './utils/constants';
|
|
17
17
|
|
|
18
18
|
export * from './utils/helpers';
|
|
19
19
|
|
|
20
|
-
export { CustomFieldDefinitionType } from './utils/validations/type';
|
|
21
20
|
/**
|
|
22
21
|
* Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
|
|
23
22
|
* @see {@link 'custom-fields/config'} for configurations
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
/* eslint-disable indent */
|
|
3
1
|
import {
|
|
4
2
|
Table,
|
|
5
3
|
Column,
|
|
@@ -12,11 +10,12 @@ import {
|
|
|
12
10
|
AfterSave,
|
|
13
11
|
Is,
|
|
14
12
|
} from 'sequelize-typescript';
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
13
|
+
import { CustomFieldDefinitionType } from '../utils/constants';
|
|
14
|
+
import { CustomValidationTypes } from '../utils/validations/validators';
|
|
17
15
|
import { CustomFieldValue } from '.';
|
|
18
16
|
import { sendDimEvent } from '../events';
|
|
19
17
|
import { UnsupportedCustomFieldTypeError, UnsupportedCustomValidationError } from '../errors';
|
|
18
|
+
import logger from '../utils/logger';
|
|
20
19
|
|
|
21
20
|
@DefaultScope(() => ({ where: { disabled: false } }))
|
|
22
21
|
@Table({
|
|
@@ -30,7 +29,10 @@ import { UnsupportedCustomFieldTypeError, UnsupportedCustomValidationError } fro
|
|
|
30
29
|
timestamps: true,
|
|
31
30
|
validate: {
|
|
32
31
|
validationByType(this: CustomFieldDefinition) {
|
|
33
|
-
|
|
32
|
+
// Verify the validation is provided if needed
|
|
33
|
+
if (!this.validation && CustomValidationTypes[this.fieldType]) {
|
|
34
|
+
// TODO: enforce the validation-object schema based on the fieldType
|
|
35
|
+
logger.error(`No custom validation for custom field type ${this.fieldType} found`);
|
|
34
36
|
throw new UnsupportedCustomValidationError(`Validation provided for "${this.fieldType}" is not supported`);
|
|
35
37
|
}
|
|
36
38
|
},
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
/* eslint-disable indent */
|
|
3
1
|
import {
|
|
4
2
|
Table,
|
|
5
3
|
Column,
|
|
@@ -14,13 +12,11 @@ import {
|
|
|
14
12
|
BeforeUpdate,
|
|
15
13
|
BeforeBulkCreate,
|
|
16
14
|
BeforeBulkUpdate,
|
|
17
|
-
Scopes,
|
|
18
15
|
} from 'sequelize-typescript';
|
|
19
16
|
import { sendDimEvent } from '../events';
|
|
20
17
|
import { CustomFieldDefinition } from '.';
|
|
21
|
-
import validateValue from '../utils/validations';
|
|
18
|
+
import { validateValue } from '../utils/validations';
|
|
22
19
|
import * as CustomFieldDefinitionRepo from '../repository/definition';
|
|
23
|
-
import logger from '../utils/logger';
|
|
24
20
|
import { InvalidValueError } from '../errors';
|
|
25
21
|
|
|
26
22
|
@Table({
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FindOptions,
|
|
3
|
-
Op, WhereOptions,
|
|
4
|
-
} from 'sequelize';
|
|
1
|
+
import { Op, type FindOptions, type WhereOptions } from 'sequelize';
|
|
5
2
|
import { CustomFieldDefinition } from '../models';
|
|
6
3
|
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
7
|
-
import { ModelOptions } from '../types';
|
|
4
|
+
import type { ModelOptions } from '../types';
|
|
8
5
|
|
|
9
6
|
export const create = (data: CreateCustomFieldDefinition): Promise<CustomFieldDefinition> =>
|
|
10
7
|
CustomFieldDefinition.create(data);
|
package/src/repository/value.ts
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
import type { FindOptions, WhereOptions } from 'sequelize';
|
|
3
3
|
import { CustomFieldValue, CustomFieldDefinition } from '../models';
|
|
4
4
|
import * as DefinitionRepo from './definition';
|
|
5
|
-
import { CreateCustomFieldValue, ValuesToUpdate } from '../types/value';
|
|
5
|
+
import type { CreateCustomFieldValue, ValuesToUpdate } from '../types/value';
|
|
6
6
|
import logger from '../utils/logger';
|
|
7
7
|
import { MissingDefinitionError } from '../errors';
|
|
8
|
-
import { ModelOptions } from '../types';
|
|
8
|
+
import type { ModelOptions } from '../types';
|
|
9
9
|
|
|
10
10
|
export const findByModelIdAndDefinition = async (modelId: string, customFieldDefinitionId: string) =>
|
|
11
11
|
CustomFieldValue.findAll({ where: { modelId, customFieldDefinitionId }, include: [CustomFieldDefinition] });
|
package/src/scopes/filter.ts
CHANGED
|
@@ -16,7 +16,7 @@ const customFieldsSearchTestFlow = async ({
|
|
|
16
16
|
fieldValue,
|
|
17
17
|
searchTerm,
|
|
18
18
|
expectedNumberOfQueryResults,
|
|
19
|
-
}: CustomFieldsSearchTestFlowInput)
|
|
19
|
+
}: CustomFieldsSearchTestFlowInput): Promise<void> => {
|
|
20
20
|
const definition = createDefinition({ fieldType, name: 'coolDefinition' });
|
|
21
21
|
await DefinitionRepo.create({ ...definition });
|
|
22
22
|
const [testModel1] = await createTestModels(definition.entityId, 2);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
import { CreateCustomFieldDefinition, CustomFieldDefinitionDTO } from '../../types/definition';
|
|
2
|
+
import type { CreateCustomFieldDefinition, CustomFieldDefinitionDTO } from '../../types/definition';
|
|
3
3
|
|
|
4
4
|
export const contextAwareFieldDefinition = {
|
|
5
5
|
name: 'cool field',
|
|
@@ -34,7 +34,7 @@ export const booleanField = (modelType: string): CreateCustomFieldDefinition =>
|
|
|
34
34
|
entityType: 'fleetId',
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
-
export const
|
|
37
|
+
export const selectField = (modelType: string, options): CreateCustomFieldDefinition => ({
|
|
38
38
|
name: 'choices',
|
|
39
39
|
modelType,
|
|
40
40
|
fieldType: 'select',
|
|
@@ -43,13 +43,11 @@ export const enumField = (modelType: string, options): CreateCustomFieldDefiniti
|
|
|
43
43
|
entityType: 'fleetId',
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
-
export const
|
|
47
|
-
name: '
|
|
46
|
+
export const statusField = (modelType: string, options): CreateCustomFieldDefinition => ({
|
|
47
|
+
name: 'lifecycle',
|
|
48
48
|
modelType,
|
|
49
|
-
fieldType: '
|
|
50
|
-
validation:
|
|
51
|
-
between: [10, 12],
|
|
52
|
-
},
|
|
49
|
+
fieldType: 'status',
|
|
50
|
+
validation: options,
|
|
53
51
|
entityId: uuidv4(),
|
|
54
52
|
entityType: 'fleetId',
|
|
55
53
|
});
|
package/src/types/index.ts
CHANGED
|
@@ -2,9 +2,22 @@ import { customFields } from '@autofleet/common-types';
|
|
|
2
2
|
|
|
3
3
|
const { CUSTOM_FIELDS_FILTER_SCOPE } = customFields;
|
|
4
4
|
|
|
5
|
-
// eslint-disable-next-line import/prefer-default-export
|
|
6
5
|
export const supportedEntities = ['businessModelId', 'fleetId', 'demandSourceId'];
|
|
7
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Supported custom field types
|
|
9
|
+
*/
|
|
10
|
+
export enum CustomFieldDefinitionType {
|
|
11
|
+
NUMBER = 'number',
|
|
12
|
+
BOOLEAN = 'boolean',
|
|
13
|
+
DATE = 'date',
|
|
14
|
+
DATETIME = 'datetime',
|
|
15
|
+
TEXT = 'text',
|
|
16
|
+
IMAGE = 'image',
|
|
17
|
+
SELECT = 'select',
|
|
18
|
+
STATUS = 'status',
|
|
19
|
+
}
|
|
20
|
+
|
|
8
21
|
export {
|
|
9
22
|
/** @deprecated Use the value from `@autofleet/common-types` instead */
|
|
10
23
|
CUSTOM_FIELDS_FILTER_SCOPE,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable import/prefer-default-export */
|
|
2
|
-
import { WhereOptions, Op, BindOrReplacements } from 'sequelize';
|
|
3
|
-
import { ModelStatic, Sequelize } from 'sequelize-typescript';
|
|
4
|
-
import { CustomFieldDefinitionType } from '../
|
|
2
|
+
import { type WhereOptions, Op, type BindOrReplacements } from 'sequelize';
|
|
3
|
+
import { type ModelStatic, Sequelize } from 'sequelize-typescript';
|
|
4
|
+
import { CustomFieldDefinitionType } from '../constants';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
@@ -28,8 +28,8 @@ interface CustomFieldsSearchPayload {
|
|
|
28
28
|
export const generateCustomFieldSearchQueryPayload = (
|
|
29
29
|
searchTerm: string,
|
|
30
30
|
model: ModelStatic,
|
|
31
|
-
entityId
|
|
32
|
-
customFieldsTypesToExclude
|
|
31
|
+
entityId: string,
|
|
32
|
+
customFieldsTypesToExclude: CustomFieldDefinitionType[] = [
|
|
33
33
|
CustomFieldDefinitionType.DATETIME,
|
|
34
34
|
CustomFieldDefinitionType.DATE,
|
|
35
35
|
],
|
package/src/utils/init.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataTypes } from 'sequelize';
|
|
2
|
-
import { ModelCtor } from 'sequelize-typescript';
|
|
2
|
+
import type { ModelCtor } from 'sequelize-typescript';
|
|
3
3
|
import { customFields } from '@autofleet/common-types';
|
|
4
4
|
import { CUSTOM_FIELDS_SORT_SCOPE } from '@autofleet/common-types/lib/custom-fields';
|
|
5
5
|
import {
|
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import type { CustomFieldDefinitionType } from '../constants';
|
|
2
|
+
import { validators } from './validators';
|
|
3
3
|
|
|
4
|
-
const validateValue = (
|
|
5
|
-
|
|
4
|
+
export const validateValue = (
|
|
5
|
+
value: unknown,
|
|
6
|
+
valueType: CustomFieldDefinitionType,
|
|
7
|
+
validation?: unknown,
|
|
8
|
+
) => {
|
|
9
|
+
const validator = validators[valueType];
|
|
10
|
+
if (!validator) {
|
|
11
|
+
// Unsupported field type
|
|
6
12
|
return false;
|
|
7
13
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return true;
|
|
14
|
+
// Always allow null values
|
|
15
|
+
return value === null || validator(value, validation);
|
|
16
|
+
/** TODO: Add validation for required fields
|
|
17
|
+
* @example
|
|
18
|
+
* if (validations.required && !value) {
|
|
19
|
+
* return false;
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
17
22
|
};
|
|
18
|
-
|
|
19
|
-
export default validateValue;
|
|
@@ -1,45 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { CustomFieldDefinitionType } from '../constants';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
+
* Validator is a function that validates a custom-field `value`,
|
|
5
|
+
* against a custom-field definition `validation object`.
|
|
6
|
+
* @returns `true` if the value is valid, `false` otherwise.
|
|
4
7
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
DATETIME = 'datetime',
|
|
11
|
-
TEXT = 'text',
|
|
12
|
-
IMAGE = 'image',
|
|
13
|
-
SELECT = 'select',
|
|
14
|
-
}
|
|
8
|
+
export type Validator<Value, DefinitionValidationObject> = (
|
|
9
|
+
value: Value,
|
|
10
|
+
validation?: DefinitionValidationObject
|
|
11
|
+
) => boolean;
|
|
12
|
+
|
|
15
13
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
14
|
+
* Validators is a map of custom-field types to their respective validators.
|
|
15
|
+
* The key is the custom-field type, and the value is the validator function.
|
|
18
16
|
*/
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// Null is always allowed
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
switch (valueType) {
|
|
26
|
-
case CustomFieldDefinitionType.TEXT:
|
|
27
|
-
return typeof value === 'string';
|
|
28
|
-
case CustomFieldDefinitionType.NUMBER:
|
|
29
|
-
return typeof value === 'number';
|
|
30
|
-
case CustomFieldDefinitionType.BOOLEAN:
|
|
31
|
-
return typeof value === 'boolean';
|
|
32
|
-
case CustomFieldDefinitionType.DATE:
|
|
33
|
-
case CustomFieldDefinitionType.DATETIME:
|
|
34
|
-
return !Joi.date().validate(value).error;
|
|
35
|
-
case CustomFieldDefinitionType.SELECT:
|
|
36
|
-
return true; // custom validation
|
|
37
|
-
case CustomFieldDefinitionType.IMAGE:
|
|
38
|
-
return !Joi.array().min(1).unique().items(Joi.string().uri())
|
|
39
|
-
.validate(value).error;
|
|
40
|
-
default:
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
17
|
+
export type Validators = {
|
|
18
|
+
[K in CustomFieldDefinitionType]: Validator<unknown, unknown>;
|
|
43
19
|
};
|
|
44
|
-
|
|
45
|
-
export default validateValueType;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import Joi from 'joi';
|
|
2
|
+
import { CustomFieldDefinitionType } from '../../constants';
|
|
3
|
+
import type { Validators } from '../type';
|
|
4
|
+
import { validateSelect } from './select.validator';
|
|
5
|
+
import { validateStatus } from './status.validator';
|
|
6
|
+
|
|
7
|
+
type CustomValidationTypes = Extract<CustomFieldDefinitionType, 'select' | 'status'>;
|
|
8
|
+
/**
|
|
9
|
+
* Custom field types that must have custom validation.
|
|
10
|
+
* TODO: remove this after moving to schema-based validation
|
|
11
|
+
*/
|
|
12
|
+
export const CustomValidationTypes = {
|
|
13
|
+
[CustomFieldDefinitionType.SELECT]: CustomFieldDefinitionType.SELECT,
|
|
14
|
+
[CustomFieldDefinitionType.STATUS]: CustomFieldDefinitionType.STATUS,
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Validators for custom fields
|
|
19
|
+
*/
|
|
20
|
+
export const validators: Validators = {
|
|
21
|
+
[CustomFieldDefinitionType.SELECT]: validateSelect,
|
|
22
|
+
[CustomFieldDefinitionType.STATUS]: validateStatus,
|
|
23
|
+
[CustomFieldDefinitionType.TEXT]: (value) => (typeof value === 'string'),
|
|
24
|
+
[CustomFieldDefinitionType.NUMBER]: (value) => (typeof value === 'number'),
|
|
25
|
+
[CustomFieldDefinitionType.BOOLEAN]: (value) => (typeof value === 'boolean'),
|
|
26
|
+
[CustomFieldDefinitionType.DATE]: (value) => (!Joi.date().validate(value).error),
|
|
27
|
+
[CustomFieldDefinitionType.DATETIME]: (value) => (!Joi.date().validate(value).error),
|
|
28
|
+
[CustomFieldDefinitionType.IMAGE]: (value) => (!Joi.array().min(1).unique()
|
|
29
|
+
.items(Joi.string().uri())
|
|
30
|
+
.validate(value).error),
|
|
31
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Validator } from '../type';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validate that the value is one of the select values
|
|
5
|
+
*/
|
|
6
|
+
export const validateSelect: Validator<string, string[]> = (
|
|
7
|
+
value,
|
|
8
|
+
selectValues,
|
|
9
|
+
) => (Array.isArray(selectValues)
|
|
10
|
+
&& selectValues.includes(value)
|
|
11
|
+
);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Validator } from '../type';
|
|
2
|
+
|
|
3
|
+
type StatusColor = string | null; // TODO: Takes from @autofleet/colors ?
|
|
4
|
+
type StatusValue = string;
|
|
5
|
+
type StatusOption = {
|
|
6
|
+
value: StatusValue;
|
|
7
|
+
color: StatusColor;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Validate that the value is one of the status values
|
|
12
|
+
*/
|
|
13
|
+
export const validateStatus: Validator<StatusValue, StatusOption[]> = (
|
|
14
|
+
value,
|
|
15
|
+
statusValues,
|
|
16
|
+
) => (Array.isArray(statusValues)
|
|
17
|
+
&& statusValues.some((status) => status.value === value)
|
|
18
|
+
);
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export declare const mustHaveCustomValidation: {
|
|
2
|
-
select: boolean;
|
|
3
|
-
};
|
|
4
|
-
/**
|
|
5
|
-
* Validates the given validations object against the supported field types and their validators.
|
|
6
|
-
* @return true if the validation is valid, false otherwise.
|
|
7
|
-
*/
|
|
8
|
-
export declare const validateValidation: (valueType: any, validation: any) => boolean;
|
|
9
|
-
/**
|
|
10
|
-
* Validates the given value against the custom validation rules for the specified field type.
|
|
11
|
-
* If no custom validation rules are provided, it falls back to the default validation.
|
|
12
|
-
* @returns true if the value is valid according to the validation rules, false otherwise.
|
|
13
|
-
*/
|
|
14
|
-
declare const customValidation: (value: any, valueType: any, validation: any) => boolean;
|
|
15
|
-
export default customValidation;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.validateValidation = exports.mustHaveCustomValidation = void 0;
|
|
7
|
-
/* eslint-disable no-shadow */
|
|
8
|
-
const logger_1 = __importDefault(require("../logger"));
|
|
9
|
-
const type_1 = require("./type");
|
|
10
|
-
const validators_1 = __importDefault(require("./validators"));
|
|
11
|
-
exports.mustHaveCustomValidation = {
|
|
12
|
-
[type_1.CustomFieldDefinitionType.SELECT]: true,
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Validates the given validations object against the supported field types and their validators.
|
|
16
|
-
* @return true if the validation is valid, false otherwise.
|
|
17
|
-
*/
|
|
18
|
-
const validateValidation = (valueType, validation) => {
|
|
19
|
-
if (!validation) {
|
|
20
|
-
if (exports.mustHaveCustomValidation[valueType]) {
|
|
21
|
-
logger_1.default.error(`No custom validation for custom field type ${valueType} found`);
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
return true;
|
|
27
|
-
};
|
|
28
|
-
exports.validateValidation = validateValidation;
|
|
29
|
-
/**
|
|
30
|
-
* Validates the given value against the custom validation rules for the specified field type.
|
|
31
|
-
* If no custom validation rules are provided, it falls back to the default validation.
|
|
32
|
-
* @returns true if the value is valid according to the validation rules, false otherwise.
|
|
33
|
-
*/
|
|
34
|
-
const customValidation = (value, valueType, validation) => {
|
|
35
|
-
const validator = validators_1.default?.[valueType];
|
|
36
|
-
if (!validation || !validator) {
|
|
37
|
-
return (0, exports.validateValidation)(valueType, validation);
|
|
38
|
-
}
|
|
39
|
-
// Always allow null values
|
|
40
|
-
return value === null || validator(value, validation);
|
|
41
|
-
};
|
|
42
|
-
exports.default = customValidation;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export declare enum CustomValidations {
|
|
2
|
-
SELECT = "select",
|
|
3
|
-
RANGE = "between"
|
|
4
|
-
}
|
|
5
|
-
type Validator = (value: any, validation: any) => boolean;
|
|
6
|
-
/**
|
|
7
|
-
* Validators for custom fields
|
|
8
|
-
*/
|
|
9
|
-
declare const validators: {
|
|
10
|
-
[key: string]: Validator;
|
|
11
|
-
};
|
|
12
|
-
export default validators;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CustomValidations = void 0;
|
|
4
|
-
// eslint-disable-next-line no-shadow
|
|
5
|
-
var CustomValidations;
|
|
6
|
-
(function (CustomValidations) {
|
|
7
|
-
CustomValidations["SELECT"] = "select";
|
|
8
|
-
CustomValidations["RANGE"] = "between";
|
|
9
|
-
})(CustomValidations = exports.CustomValidations || (exports.CustomValidations = {}));
|
|
10
|
-
/**
|
|
11
|
-
* Validate {@link CustomValidations.ENUM Enum}
|
|
12
|
-
*/
|
|
13
|
-
const validateEnum = (value, enumValues) => (Array.isArray(enumValues)
|
|
14
|
-
&& enumValues.length > 0
|
|
15
|
-
&& enumValues.includes(value));
|
|
16
|
-
/**
|
|
17
|
-
* Validate {@link CustomValidations.RANGE Range}
|
|
18
|
-
*/
|
|
19
|
-
const validateRange = (value, range) => {
|
|
20
|
-
const [min, max] = range;
|
|
21
|
-
if (min === undefined || max === undefined) {
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
return value >= range.min && value <= range.max;
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* Validators for custom fields
|
|
28
|
-
*/
|
|
29
|
-
const validators = {
|
|
30
|
-
[CustomValidations.SELECT]: validateEnum,
|
|
31
|
-
[CustomValidations.RANGE]: validateRange,
|
|
32
|
-
};
|
|
33
|
-
exports.default = validators;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-shadow */
|
|
2
|
-
import logger from '../logger';
|
|
3
|
-
import { CustomFieldDefinitionType } from './type';
|
|
4
|
-
import validators from './validators';
|
|
5
|
-
|
|
6
|
-
export const mustHaveCustomValidation = {
|
|
7
|
-
[CustomFieldDefinitionType.SELECT]: true,
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Validates the given validations object against the supported field types and their validators.
|
|
12
|
-
* @return true if the validation is valid, false otherwise.
|
|
13
|
-
*/
|
|
14
|
-
export const validateValidation = (valueType, validation) => {
|
|
15
|
-
if (!validation) {
|
|
16
|
-
if (mustHaveCustomValidation[valueType]) {
|
|
17
|
-
logger.error(`No custom validation for custom field type ${valueType} found`);
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
return true;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Validates the given value against the custom validation rules for the specified field type.
|
|
27
|
-
* If no custom validation rules are provided, it falls back to the default validation.
|
|
28
|
-
* @returns true if the value is valid according to the validation rules, false otherwise.
|
|
29
|
-
*/
|
|
30
|
-
const customValidation = (value, valueType, validation) => {
|
|
31
|
-
const validator = validators?.[valueType];
|
|
32
|
-
if (!validation || !validator) {
|
|
33
|
-
return validateValidation(valueType, validation);
|
|
34
|
-
}
|
|
35
|
-
// Always allow null values
|
|
36
|
-
return value === null || validator(value, validation);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export default customValidation;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line no-shadow
|
|
2
|
-
export enum CustomValidations {
|
|
3
|
-
SELECT = 'select',
|
|
4
|
-
RANGE = 'between'
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
type Validator = (value, validation) => boolean;
|
|
8
|
-
/**
|
|
9
|
-
* Validate {@link CustomValidations.ENUM Enum}
|
|
10
|
-
*/
|
|
11
|
-
const validateEnum: Validator = (value, enumValues) => (Array.isArray(enumValues)
|
|
12
|
-
&& enumValues.length > 0
|
|
13
|
-
&& enumValues.includes(value)
|
|
14
|
-
);
|
|
15
|
-
/**
|
|
16
|
-
* Validate {@link CustomValidations.RANGE Range}
|
|
17
|
-
*/
|
|
18
|
-
const validateRange: Validator = (value, range) => {
|
|
19
|
-
const [min, max] = range;
|
|
20
|
-
if (min === undefined || max === undefined) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
return value >= range.min && value <= range.max;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Validators for custom fields
|
|
28
|
-
*/
|
|
29
|
-
const validators: { [key: string]: Validator } = {
|
|
30
|
-
[CustomValidations.SELECT]: validateEnum,
|
|
31
|
-
[CustomValidations.RANGE]: validateRange,
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export default validators;
|
|
File without changes
|