Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 4x 4x 9x 13x 4x 4x 4x 4x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 3x 3x 4x 4x 1x 1x 8x 1x 1x 7x 1x 1x 1x 1x 1x 6x 6x 6x 2x 2x 4x 4x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 25x 25x 25x 25x 25x 25x 25x 25x 21x 21x 21x 21x 1x 1x 21x 21x 21x 21x 21x 21x 21x 21x 21x 3x 3x 21x 1x | import * as Yup from 'yup';
import { type RecordContent } from '@amalia/data-capture/connectors/types';
import { FormatsEnum } from '@amalia/data-capture/fields/types';
import { type Properties } from '@amalia/data-capture/record-models/types';
import { dayjs } from '@amalia/ext/dayjs';
import { CurrencySymbolsEnum } from '@amalia/ext/iso-4217';
import { toError } from '@amalia/ext/typescript';
// Convert formatsEnum to Yup schema
type FormatEnumSchemaType =
| Yup.ArraySchema<unknown[] | null | undefined, Yup.AnyObject, undefined, ''>
| Yup.ArraySchema<unknown[], Yup.AnyObject, unknown[], ''>
| Yup.BooleanSchema<boolean | null | undefined, Yup.AnyObject, undefined, ''>
| Yup.NumberSchema<number | null | undefined, Yup.AnyObject, undefined, ''>
| Yup.ObjectSchema<
{ symbol: CurrencySymbolsEnum; value: number | null } | null | undefined,
Yup.AnyObject,
{ symbol: CurrencySymbolsEnum.EUR; value: null },
''
>
| Yup.StringSchema<string | null | undefined, Yup.AnyObject, undefined, ''>;
const parseTimestampValue = (value: unknown) => {
if (typeof value === 'number') {
return value >= 1_000_000_000_000 ? dayjs(value) : dayjs.unix(value);
}
if (typeof value === 'string' && /^\d+$/u.test(value)) {
const timestamp = Number(value);
return timestamp >= 1_000_000_000_000 ? dayjs(timestamp) : dayjs.unix(timestamp);
}
return null;
};
const typeMap: Record<FormatsEnum, FormatEnumSchemaType> = {
[FormatsEnum.percent]: Yup.number()
.nullable()
.optional()
.transform((value: unknown, original?: unknown): unknown =>
original === '' || Number.isNaN(original) ? null : value,
),
[FormatsEnum.currency]: Yup.object()
.shape({
symbol: Yup.string().oneOf(Object.values(CurrencySymbolsEnum)).default(CurrencySymbolsEnum.EUR),
value: Yup.number()
.optional()
.transform((value: unknown, original: unknown): unknown =>
original === '' || Number.isNaN(original) ? null : value,
)
.nullable()
.default(null),
})
.nullable()
.optional(),
[FormatsEnum.number]: Yup.number()
.nullable()
.optional()
.transform((value: unknown, original?: unknown) => (original === '' || Number.isNaN(original) ? null : value)),
[FormatsEnum.date]: Yup.string()
.transform((value: unknown, originalValue: unknown): unknown => {
const timestampValue = parseTimestampValue(originalValue);
if (!timestampValue) {
return value;
}
return timestampValue.format('YYYY-MM-DD');
})
.test('is-date', "String doesn't match date validation", (value) => {
if (!value) {
return true;
}
return dayjs(value, 'YYYY-MM-DD', true).isValid();
})
.nullable()
.optional(),
[FormatsEnum['date-time']]: Yup.string()
.transform((value: unknown, originalValue: unknown): unknown => {
const timestampValue = parseTimestampValue(originalValue);
if (!timestampValue) {
return value;
}
return timestampValue.utc().format('YYYY-MM-DDTHH:mm:ss[Z]');
})
.test('is-date-time', "String doesn't match date-time validation", (value) => {
if (!value) {
return true;
}
// Parse in ISO 8601 format
return (
dayjs(
value,
[
'YYYY-MM-DDTHH:mm:ss.SSS[Z]',
'YYYY-MM-DDTHH:mm:ss.SSSZZ',
'YYYY-MM-DDTHH:mm:ss.SSSZ',
'YYYY-MM-DDTHH:mm:ss.SSS',
'YYYY-MM-DDTHH:mm:ss[Z]',
'YYYY-MM-DDTHH:mm:ssZZ',
'YYYY-MM-DDTHH:mm:ssZ',
'YYYY-MM-DDTHH:mm:ss',
],
true,
).isValid() ||
// For this special case, don't use strict mode.
// A bug ticket is opened there https://github.com/iamkun/dayjs/issues/2778
// Tracked internally in https://gitlab.com/amal-ia/amalia-web/-/issues/7325
dayjs(value, ['YYYY-MM-DDTHH:mm:ssZ'], false).isValid()
);
})
.nullable()
.optional(),
[FormatsEnum.text]: Yup.string().nullable().optional(),
[FormatsEnum.table]: Yup.array().nullable().optional(),
[FormatsEnum.boolean]: Yup.boolean().nullable().optional(),
};
export class RowValidator {
private static convertSchemaToYup(schema: Properties) {
const shape: Record<string, Yup.AnySchema> = Object.entries(schema).reduce((acc, [key, property]) => {
const { format, isRequired } = property!;
const propertySchema: FormatEnumSchemaType = typeMap[format];
return {
...acc,
[key]: isRequired ? propertySchema.required() : propertySchema,
};
}, {});
return Yup.object().shape(shape);
}
public static async validate(row: object, schema: Properties): Promise<RecordContent> {
const yupSchema = RowValidator.convertSchemaToYup(schema);
try {
return await yupSchema.validate(row, {
// Collect all errors then Throw
abortEarly: false,
// Remove unspecified keys from objects
stripUnknown: true,
});
} catch (error) {
throw toError(error);
}
}
}
|