@progress/kendo-angular-barcodes
Version:
Kendo UI Angular Barcodes
51 lines (50 loc) • 1.84 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 validator for a specific Barcode type.
*
* Use this function to validate a Barcode value for a given `BarcodeType`.
*
* @param type Specifies the type of the Barcode.
* @param size Specifies the size of the barcode, excluding the text label, padding, and border. This parameter is optional.
* @returns Returns a validator function. The function returns an error map with the `barcode` property if validation fails. Otherwise, it returns `null` if valid.
*
* @example
* ```typescript
* 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
}
};
};
};