src/utilities.client.js
function validateProxyField(fieldKey, value, isRequired = false, options = null) {
    const fieldErrors = [];
    if (isRequired) {
        // Nullable error is already handled by AJV
        if (value === null) return fieldErrors;
        if (!value) {
            const message = m('inputSchema.validation.required', { fieldKey });
            fieldErrors.push(message);
            return fieldErrors;
        }

        const { useApifyProxy, proxyUrls } = value;
        if (!useApifyProxy && (!Array.isArray(proxyUrls) || proxyUrls.length === 0)) {
            fieldErrors.push(m('inputSchema.validation.proxyRequired', { fieldKey }));
            return fieldErrors;
        }
    }

    // Input is not required, so missing value is valid
    if (!value) return fieldErrors;

    const { useApifyProxy, proxyUrls, apifyProxyGroups } = value;

    if (!useApifyProxy && Array.isArray(proxyUrls)) {
        let invalidUrl = false;
        proxyUrls.forEach((url) => {
            if (!regex.PROXY_URL_REGEX.test(url.trim())) invalidUrl = url.trim();
        });
        if (invalidUrl) {
            fieldErrors.push(m('inputSchema.validation.customProxyInvalid', { invalidUrl }));
        }
    }

    // If Apify proxy is not used skip additional checks
    if (!useApifyProxy) return fieldErrors;

    // If options are not provided skip additional checks
    if (!options) return fieldErrors;

    const selectedProxyGroups = (apifyProxyGroups || []);

    // Auto mode, check that user has access to alteast one proxy group usable in this mode
    if (!selectedProxyGroups.length && !options.hasAutoProxyGroups) {
        fieldErrors.push(m('inputSchema.validation.noAvailableAutoProxy'));
        return fieldErrors;
    }

    // Check if proxy groups selected by user are available to him
    const availableProxyGroupsById = {};
    (options.availableProxyGroups || []).forEach((group) => { availableProxyGroupsById[group] = true; });
    const unavailableProxyGroups = selectedProxyGroups.filter(group => !availableProxyGroupsById[group]);

    if (unavailableProxyGroups.length) {
        fieldErrors.push(m('inputSchema.validation.proxyGroupsNotAvailable', { fieldKey, groups: unavailableProxyGroups.join(', ') }));
    }

    // Check if any of the proxy groups are blocked and if yes then output the associated message
    const blockedProxyGroupsById = options.disabledProxyGroups || {};
    selectedProxyGroups.filter(group => blockedProxyGroupsById[group]).forEach((blockedGroup) => {
        fieldErrors.push(blockedProxyGroupsById[blockedGroup]);
    });

    return fieldErrors;
}
function validateProxyField(fieldKey, value, isRequired = false, options = null) {
    const fieldErrors = [];
    if (isRequired) {
        // Nullable error is already handled by AJV
        if (value === null) return fieldErrors;
        if (!value) {
            const message = m('inputSchema.validation.required', { fieldKey });
            fieldErrors.push(message);
            return fieldErrors;
        }

        const { useApifyProxy, proxyUrls } = value;
        if (!useApifyProxy && (!Array.isArray(proxyUrls) || proxyUrls.length === 0)) {
            fieldErrors.push(m('inputSchema.validation.proxyRequired', { fieldKey }));
            return fieldErrors;
        }
    }

    // Input is not required, so missing value is valid
    if (!value) return fieldErrors;

    const { useApifyProxy, proxyUrls, apifyProxyGroups } = value;

    if (!useApifyProxy && Array.isArray(proxyUrls)) {
        let invalidUrl = false;
        proxyUrls.forEach((url) => {
            if (!regex.PROXY_URL_REGEX.test(url.trim())) invalidUrl = url.trim();
        });
        if (invalidUrl) {
            fieldErrors.push(m('inputSchema.validation.customProxyInvalid', { invalidUrl }));
        }
    }

    // If Apify proxy is not used skip additional checks
    if (!useApifyProxy) return fieldErrors;

    // If options are not provided skip additional checks
    if (!options) return fieldErrors;

    const selectedProxyGroups = (apifyProxyGroups || []);

    // Auto mode, check that user has access to alteast one proxy group usable in this mode
    if (!selectedProxyGroups.length && !options.hasAutoProxyGroups) {
        fieldErrors.push(m('inputSchema.validation.noAvailableAutoProxy'));
        return fieldErrors;
    }

    // Check if proxy groups selected by user are available to him
    const availableProxyGroupsById = {};
    (options.availableProxyGroups || []).forEach((group) => { availableProxyGroupsById[group] = true; });
    const unavailableProxyGroups = selectedProxyGroups.filter(group => !availableProxyGroupsById[group]);

    if (unavailableProxyGroups.length) {
        fieldErrors.push(m('inputSchema.validation.proxyGroupsNotAvailable', { fieldKey, groups: unavailableProxyGroups.join(', ') }));
    }

    // Check if any of the proxy groups are blocked and if yes then output the associated message
    const blockedProxyGroupsById = options.disabledProxyGroups || {};
    selectedProxyGroups.filter(group => blockedProxyGroupsById[group]).forEach((blockedGroup) => {
        fieldErrors.push(blockedProxyGroupsById[blockedGroup]);
    });

    return fieldErrors;
}
function validateProxyField ( fieldKey , value , isRequired = false , options = null ) { const fieldErrors = [ ] ; if ( isRequired ) { // Nullable error is already handled by AJV if ( value === null ) return fieldErrors ; if ( ! value ) { const message = m ( 'inputSchema.validation.required' , { fieldKey } ) ; fieldErrors . push ( message ) ; return fieldErrors ; } const { useApifyProxy , proxyUrls } = value ; if ( ! useApifyProxy && ( ! Array . isArray ( proxyUrls ) || proxyUrls . length === 0 ) ) { fieldErrors . push ( m ( 'inputSchema.validation.proxyRequired' , { fieldKey } ) ) ; return fieldErrors ; } } // Input is not required, so missing value is valid if ( ! value ) return fieldErrors ; const { useApifyProxy , proxyUrls , apifyProxyGroups } = value ; if ( ! useApifyProxy && Array . isArray ( proxyUrls ) ) { let invalidUrl = false ; proxyUrls . forEach ( ( url ) => { if ( ! regex . PROXY_URL_REGEX . test ( url . trim ( ) ) ) invalidUrl = url . trim ( ) ; } ) ; if ( invalidUrl ) { fieldErrors . push ( m ( 'inputSchema.validation.customProxyInvalid' , { invalidUrl } ) ) ; } } // If Apify proxy is not used skip additional checks if ( ! useApifyProxy ) return fieldErrors ; // If options are not provided skip additional checks if ( ! options ) return fieldErrors ; const selectedProxyGroups = ( apifyProxyGroups || [ ] ) ; // Auto mode, check that user has access to alteast one proxy group usable in this mode if ( ! selectedProxyGroups . length && ! options . hasAutoProxyGroups ) { fieldErrors . push ( m ( 'inputSchema.validation.noAvailableAutoProxy' ) ) ; return fieldErrors ; } // Check if proxy groups selected by user are available to him const availableProxyGroupsById = { } ; ( options . availableProxyGroups || [ ] ) . forEach ( ( group ) => { availableProxyGroupsById [ group ] = true ; } ) ; const unavailableProxyGroups = selectedProxyGroups . filter ( group => ! availableProxyGroupsById [ group ] ) ; if ( unavailableProxyGroups . length ) { fieldErrors . push ( m ( 'inputSchema.validation.proxyGroupsNotAvailable' , { fieldKey , groups : unavailableProxyGroups . join ( ', ' ) } ) ) ; } // Check if any of the proxy groups are blocked and if yes then output the associated message const blockedProxyGroupsById = options . disabledProxyGroups || { } ; selectedProxyGroups . filter ( group => blockedProxyGroupsById [ group ] ) . forEach ( ( blockedGroup ) => { fieldErrors . push ( blockedProxyGroupsById [ blockedGroup ] ) ; } ) ; return fieldErrors ; }
Validate's input field configured with proxy editor
@param {Object} fieldKey Proxy field value
@param {Object} value Proxy field value
@param {Boolean} isRequired Whether the field is required or not
@param {Object} options (Optional) Information about proxy groups availability
@param {Boolean} options.hasAutoProxyGroups Informs validation whether user has atleast one proxy group available in auto mode
@param {Array<String>} options.availableProxyGroups List of available proxy groups
@param {Object} options.disabledProxyGroups Object with groupId as key and error message as value (mostly for residential/SERP)
Validate s input field configured with proxy editor