|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any, no-use-before-define */ |
| 2 | + |
| 3 | +import { PlatformConfig } from './types' |
| 4 | + |
| 5 | +interface ValidationResult { |
| 6 | + errors: string[] |
| 7 | + valid: boolean |
| 8 | +} |
| 9 | + |
| 10 | +export function isPlatformConfig(obj: any): obj is PlatformConfig { |
| 11 | + const result = validatePlatformConfig(obj) |
| 12 | + return result.valid |
| 13 | +} |
| 14 | + |
| 15 | +export function validatePlatformConfig(config: any): ValidationResult { |
| 16 | + const errors: string[] = [] |
| 17 | + |
| 18 | + if (!config || typeof config !== 'object') { |
| 19 | + return { valid: false, errors: ['Configuration must be an object'] } |
| 20 | + } |
| 21 | + |
| 22 | + if (!Array.isArray(config.ios)) { |
| 23 | + errors.push('config.ios: must be an array') |
| 24 | + } else { |
| 25 | + config.ios.forEach((app: any, index: number) => { |
| 26 | + errors.push(...validateAppConfig(app, `config.ios[${index}]`)) |
| 27 | + }) |
| 28 | + } |
| 29 | + |
| 30 | + if (!Array.isArray(config.android)) { |
| 31 | + errors.push('config.android: must be an array of applications') |
| 32 | + } else { |
| 33 | + config.android.forEach((app: any, index: number) => { |
| 34 | + errors.push(...validateAppConfig(app, `config.android[${index}]`)) |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + if (!Array.isArray(config.pc)) { |
| 39 | + errors.push('config.pc: must be an array of applications') |
| 40 | + } else { |
| 41 | + config.pc.forEach((app: any, index: number) => { |
| 42 | + errors.push(...validateAppConfig(app, `config.pc[${index}]`)) |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + return { |
| 47 | + valid: errors.length === 0, |
| 48 | + errors |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function validateAppConfig(app: any, path: string): string[] { |
| 53 | + const errors: string[] = [] |
| 54 | + |
| 55 | + if (!app || typeof app !== 'object') { |
| 56 | + return [`${path}: must be an object`] |
| 57 | + } |
| 58 | + |
| 59 | + if (typeof app.id !== 'string') { |
| 60 | + errors.push(`${path}.id: must be a lowercase string`) |
| 61 | + } else if (!/^[a-z]/.test(app.id)) { |
| 62 | + errors.push(`${path}.id: must start with a lowercase letter`) |
| 63 | + } |
| 64 | + |
| 65 | + if (typeof app.name !== 'string') errors.push(`${path}.name: must be a string`) |
| 66 | + if (typeof app.isFeatured !== 'boolean') errors.push(`${path}.isFeatured: must be a boolean`) |
| 67 | + if (typeof app.urlScheme !== 'string') errors.push(`${path}.urlScheme: must be a string`) |
| 68 | + |
| 69 | + if (app.isNeedBase64Encoding !== undefined && typeof app.isNeedBase64Encoding !== 'boolean') { |
| 70 | + errors.push(`${path}.isNeedBase64Encoding: must be a boolean`) |
| 71 | + } |
| 72 | + |
| 73 | + if (!app.installationStep || typeof app.installationStep !== 'object') { |
| 74 | + errors.push(`${path}.installationStep: must be an object`) |
| 75 | + } else { |
| 76 | + errors.push( |
| 77 | + ...validateLocalizedText( |
| 78 | + app.installationStep.description, |
| 79 | + `${path}.installationStep.description` |
| 80 | + ) |
| 81 | + ) |
| 82 | + errors.push( |
| 83 | + ...validateButtons(app.installationStep.buttons, `${path}.installationStep.buttons`) |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + if (!app.addSubscriptionStep) { |
| 88 | + errors.push(`${path}.addSubscriptionStep: required field is missing`) |
| 89 | + } else { |
| 90 | + errors.push(...validateStep(app.addSubscriptionStep, `${path}.addSubscriptionStep`)) |
| 91 | + } |
| 92 | + |
| 93 | + if (!app.connectAndUseStep) { |
| 94 | + errors.push(`${path}.connectAndUseStep: required field is missing`) |
| 95 | + } else { |
| 96 | + errors.push(...validateStep(app.connectAndUseStep, `${path}.connectAndUseStep`)) |
| 97 | + } |
| 98 | + |
| 99 | + if (app.additionalBeforeAddSubscriptionStep) { |
| 100 | + errors.push( |
| 101 | + ...validateTitleStep( |
| 102 | + app.additionalBeforeAddSubscriptionStep, |
| 103 | + `${path}.additionalBeforeAddSubscriptionStep` |
| 104 | + ) |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + if (app.additionalAfterAddSubscriptionStep) { |
| 109 | + errors.push( |
| 110 | + ...validateTitleStep( |
| 111 | + app.additionalAfterAddSubscriptionStep, |
| 112 | + `${path}.additionalAfterAddSubscriptionStep` |
| 113 | + ) |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + return errors |
| 118 | +} |
| 119 | + |
| 120 | +function validateButton(button: any, path: string): string[] { |
| 121 | + const errors: string[] = [] |
| 122 | + |
| 123 | + if (!button || typeof button !== 'object') { |
| 124 | + return [`${path}: button must be an object`] |
| 125 | + } |
| 126 | + |
| 127 | + if (typeof button.buttonLink !== 'string') { |
| 128 | + errors.push(`${path}.buttonLink: must be a string`) |
| 129 | + } else if (button.buttonLink === '') { |
| 130 | + errors.push(`${path}.buttonLink: can't be empty`) |
| 131 | + } |
| 132 | + |
| 133 | + errors.push(...validateLocalizedText(button.buttonText, `${path}.buttonText`)) |
| 134 | + |
| 135 | + return errors |
| 136 | +} |
| 137 | + |
| 138 | +function validateButtons(buttons: any, path: string): string[] { |
| 139 | + const errors: string[] = [] |
| 140 | + |
| 141 | + if (!Array.isArray(buttons)) { |
| 142 | + return [`${path}: must be an array of buttons`] |
| 143 | + } |
| 144 | + |
| 145 | + buttons.forEach((button, index) => { |
| 146 | + errors.push(...validateButton(button, `${path}[${index}]`)) |
| 147 | + }) |
| 148 | + |
| 149 | + return errors |
| 150 | +} |
| 151 | + |
| 152 | +function validateLocalizedText(text: any, path: string): string[] { |
| 153 | + const errors: string[] = [] |
| 154 | + |
| 155 | + if (!text || typeof text !== 'object') { |
| 156 | + return [`${path}: must be an object with translations`] |
| 157 | + } |
| 158 | + |
| 159 | + if (typeof text.en !== 'string') errors.push(`${path}.en: must be a string`) |
| 160 | + if (typeof text.fa !== 'string') errors.push(`${path}.fa: must be a string`) |
| 161 | + if (typeof text.ru !== 'string') errors.push(`${path}.ru: must be a string`) |
| 162 | + |
| 163 | + if (typeof text.en === 'string' && text.en === '') errors.push(`${path}.en: can't be empty`) |
| 164 | + if (typeof text.fa === 'string' && text.fa === '') errors.push(`${path}.fa: can't be empty`) |
| 165 | + if (typeof text.ru === 'string' && text.ru === '') errors.push(`${path}.ru: can't be empty`) |
| 166 | + |
| 167 | + return errors |
| 168 | +} |
| 169 | + |
| 170 | +function validateStep(step: any, path: string): string[] { |
| 171 | + const errors: string[] = [] |
| 172 | + |
| 173 | + if (!step || typeof step !== 'object') { |
| 174 | + return [`${path}: must be an object`] |
| 175 | + } |
| 176 | + |
| 177 | + errors.push(...validateLocalizedText(step.description, `${path}.description`)) |
| 178 | + |
| 179 | + return errors |
| 180 | +} |
| 181 | + |
| 182 | +function validateTitleStep(step: any, path: string): string[] { |
| 183 | + const errors: string[] = [] |
| 184 | + |
| 185 | + if (!step || typeof step !== 'object') { |
| 186 | + return [`${path}: must be an object`] |
| 187 | + } |
| 188 | + |
| 189 | + errors.push(...validateLocalizedText(step.title, `${path}.title`)) |
| 190 | + errors.push(...validateLocalizedText(step.description, `${path}.description`)) |
| 191 | + errors.push(...validateButtons(step.buttons, `${path}.buttons`)) |
| 192 | + |
| 193 | + return errors |
| 194 | +} |
0 commit comments