private validateCertInfo()

in source/packages/services/bulkcerts/src/certificates/certificatestask.service.ts [215:292]


    private validateCertInfo(certInfo: CertificateInfo): CertInfoValidationResult {
        logger.debug(
            `certificatestask.service validateCertInfo: in: certInfo:${JSON.stringify(certInfo)}`
        );

        const commonNameRE = /^[0-9A-Fa-f]+$/g;
        // remove any undefined properties from the input document
        const docAsJson = JSON.parse(JSON.stringify(certInfo));

        Object.keys(docAsJson).forEach((k) => {
            if (docAsJson[k] === undefined) {
                delete docAsJson[k];
            }
            if (k === 'commonName') {
                Object.keys(docAsJson[k]).forEach((k2) => {
                    if (docAsJson[k][k2] === undefined) {
                        delete docAsJson[k][k2];
                    }
                });
            } else if (k === 'country') {
                ow(docAsJson[k], 'country', ow.string.length(2));
            }
        });
        certInfo = docAsJson;
        const result = new CertInfoValidationResult();
        result.isValid = true;

        // Generator exists and value is valid
        // RangeStart
        if (typeof certInfo['commonName'] !== 'undefined') {
            certInfo = this.constructCommonName(certInfo);
            const commonName = certInfo['commonName'];
            if (
                typeof commonName['generator'] !== 'undefined' &&
                ['list', 'increment', 'static'].includes(commonName['generator'])
            ) {
                const generator = commonName['generator'];
                if (generator === 'increment') {
                    if (typeof commonName['commonNameStart'] !== 'undefined') {
                        if (!commonNameRE.test(commonName['commonNameStart'])) {
                            result.isValid = false;
                            result.errors['ArgumentError'] =
                                'certInfo/commonName string should contain a hexadecimal value';
                        }
                    } else {
                        result.isValid = false;
                        result.errors['ArgumentError'] =
                            'certInfo/commonName increment section missing value';
                    }
                } else if (generator === 'list') {
                    if (typeof certInfo['commonNameList'] !== 'undefined') {
                        const names: string[] = certInfo['commonNameList'];
                        if (names.length === 0) {
                            result.isValid = false;
                            result.errors['ArgumentError'] =
                                'certInfo/commonNameList property should be an array of string values';
                        }
                    } else {
                        result.isValid = false;
                        result.errors['ArgumentError'] =
                            'certInfo/commonNameList property missing';
                    }
                } else if (generator === 'static') {
                    if (typeof commonName['commonNameStatic'] === 'undefined') {
                        result.isValid = false;
                        result.errors['ArgumentError'] = 'certinfo/commonName property missing';
                    }
                }
            } else {
                result.isValid = false;
                result.errors['ArgumentError'] =
                    'certinfo/commonName Generator section missing or invalid';
            }
        }

        logger.debug(`certificatestask.service validateCertInfo: exit:${JSON.stringify(result)}`);
        return result;
    }