Skip to content

Commit 93bbed3

Browse files
fix(validation): tolerate runtime validation payload variations
Signed-off-by: Vitor Mattos <[email protected]>
1 parent ed2c527 commit 93bbed3

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

src/services/validationDocument.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ function isOptionalField(record: UnknownRecord, key: string, guard: (value: unkn
6464
}
6565

6666
function toNumber(value: unknown): number | null {
67-
return typeof value === 'number' && Number.isFinite(value) ? value : null
67+
if (typeof value === 'number' && Number.isFinite(value)) {
68+
return value
69+
}
70+
71+
if (typeof value === 'string' && /^-?\d+$/.test(value)) {
72+
return Number.parseInt(value, 10)
73+
}
74+
75+
return null
6876
}
6977

7078
function isString(value: unknown): value is string {
@@ -91,6 +99,15 @@ function isSignerStatus(value: unknown): value is SignerDetailRecord['status'] {
9199
|| normalizedValue === SIGN_REQUEST_STATUS.SIGNED
92100
}
93101

102+
function isValidationSignatureFlow(value: unknown): boolean {
103+
if (value === 'none' || value === 'parallel' || value === 'ordered_numeric') {
104+
return true
105+
}
106+
107+
const normalizedValue = toNumber(value)
108+
return normalizedValue === 0 || normalizedValue === 1 || normalizedValue === 2
109+
}
110+
94111
function isValidationStatusInfo(value: unknown): value is ValidationStatusInfo {
95112
if (!isRecord(value)) {
96113
return false
@@ -210,12 +227,12 @@ function isValidationDocumentRecord(data: unknown): data is ValidationFileRecord
210227
|| !isString(data.statusText)
211228
|| typeof data.nodeId !== 'number'
212229
|| (data.nodeType !== 'file' && data.nodeType !== 'envelope')
213-
|| typeof data.signatureFlow !== 'number'
214-
|| typeof data.docmdpLevel !== 'number'
215-
|| typeof data.filesCount !== 'number'
230+
|| !isValidationSignatureFlow(data.signatureFlow)
231+
|| toNumber(data.docmdpLevel) === null
232+
|| toNumber(data.filesCount) === null
216233
|| !Array.isArray(data.files)
217-
|| typeof data.totalPages !== 'number'
218-
|| typeof data.size !== 'number'
234+
|| toNumber(data.totalPages) === null
235+
|| toNumber(data.size) === null
219236
|| !isString(data.pdfVersion)
220237
|| !isString(data.created_at)
221238
|| !isRequestedBy(data.requested_by)

0 commit comments

Comments
 (0)