@progress/kendo-angular-barcodes
Version:
Kendo UI Angular Barcodes
50 lines (49 loc) • 1.74 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { barcodeValidator } from '@progress/kendo-charts';
/**
* @hidden
*/
const isPresent = (value) => value !== null && value !== undefined;
/**
* Creates a value validator for a particular Barcode type.
*
* @param {BarcodeType} type The type of the Barcode.
* @param {Size} size The size of the barcode, excluding the text label, padding and border. Optional.
* @returns {ValidatorFn} A validator function that returns an error map with the `barcode` property if the validation check fails, otherwise `null`.
*
* @example
* ```ts-no-run
* const control = new FormControl('1234', createBarcodeValidator('EAN8'));
* console.log(control.errors);
*
* // {
* // barcode: {
* // message: 'The value of the "EAN13" encoding should be 12 symbols',
* // value: '1234',
* // type: 'EAN13'
* // }
* // }
* ```
*/
export const createBarcodeValidator = (type, size) => {
const validator = barcodeValidator(type, size);
return (control) => {
if (!isPresent(control.value) || control.value === '') {
return null;
}
const result = validator(control.value);
if (result.valid === true) {
return null;
}
return {
barcode: {
message: result.error.message,
value: control.value,
type: type
}
};
};
};