Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add PersonalDataAndDocumentsDetailed kyc schema #122

Merged
merged 6 commits into from Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/common.ts
Expand Up @@ -91,3 +91,6 @@ export enum CryptoType {
export const cryptoTypeSchema = z.nativeEnum(CryptoType, {
description: 'cryptoTypeSchema',
})
export const EMAIL_REGEX =
/* eslint-disable-next-line no-useless-escape */ // For some reason, eslint thinks the escaped \[ and /] are useless. they are indeed useful.
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ // credit to http://emailregex.com/
6 changes: 2 additions & 4 deletions src/fiat-account.ts
@@ -1,4 +1,5 @@
import { z } from 'zod'
import { EMAIL_REGEX } from './common'

/*
* FiatConnect dynamic type definitions.
Expand Down Expand Up @@ -47,9 +48,6 @@ const requiredFiatAccountSchemaFieldsSchema = z.object({
fiatAccountType: fiatAccountTypeSchema,
})

export const PIX_EMAIL_KEY_REGEX =
/* eslint-disable-next-line no-useless-escape */ // For some reason, eslint thinks the escaped \[ and /] are useless. they are indeed useful.
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ // credit to http://emailregex.com/
export const PIX_CPF_KEY_REGEX = /^([0-9]{3}\.){2}[0-9]{3}[-]([0-9]{2})$/ // example: 000.000.000-00, see https://en.wikipedia.org/wiki/CPF_number
export const PIX_PHONE_KEY_REGEX = /^[0-9]{11}$/
export const PIX_RANDOM_KEY_REGEX = /^[a-zA-Z0-9-]{32}$/
Expand All @@ -67,7 +65,7 @@ export const pixAccountSchema = requiredFiatAccountSchemaFieldsSchema
z
.object({
keyType: z.literal(PIXKeyTypeEnum.EMAIL),
key: z.string().regex(PIX_EMAIL_KEY_REGEX),
key: z.string().regex(EMAIL_REGEX),
})
.or(
z.object({
Expand Down
64 changes: 64 additions & 0 deletions src/kyc.ts
@@ -1,4 +1,5 @@
import { z } from 'zod'
import { EMAIL_REGEX } from './common'

export enum KycStatus {
KycNotCreated = 'KycNotCreated',
Expand All @@ -7,6 +8,7 @@ export enum KycStatus {
KycDenied = 'KycDenied',
KycExpired = 'KycExpired',
}

export const kycStatusSchema = z.nativeEnum(KycStatus, {
description: 'kycStatusSchema',
})
Expand All @@ -23,11 +25,44 @@ export const kycStatusSchema = z.nativeEnum(KycStatus, {
// When adding new schemas be sure to also update kycSchemasSchema
export enum KycSchema {
PersonalDataAndDocuments = 'PersonalDataAndDocuments',
PersonalDataAndDocumentsDetailed = 'PersonalDataAndDocumentsDetailed',
}

export const kycSchemaSchema = z.nativeEnum(KycSchema, {
description: 'kycSchemaSchema',
})

export enum IdentificationDocumentType {
IDC = 'IDC',
PAS = 'PAS',
DL = 'DL',
}

// need nonempty array types to get zod enums to compile
const documentsWithBack: [
IdentificationDocumentType,
...IdentificationDocumentType[],
] = [IdentificationDocumentType.IDC, IdentificationDocumentType.DL]
const documentsWithoutBack: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and the award for most uses of the word "document" in a single expression goes to... 🤣

IdentificationDocumentType,
...IdentificationDocumentType[],
] = (
Object.keys(IdentificationDocumentType) as [
IdentificationDocumentType,
...IdentificationDocumentType[],
]
).filter((idType) => !documentsWithBack.includes(idType)) as [
IdentificationDocumentType,
...IdentificationDocumentType[],
]

const identificationDocumentTypeWithBackSchema = z.enum(documentsWithBack)
const identificationDocumentTypeWithoutBackSchema = z.enum(documentsWithoutBack)
export const identificationDocumentTypeSchema =
identificationDocumentTypeWithBackSchema.or(
identificationDocumentTypeWithoutBackSchema,
)

export const personalDataAndDocumentsKycSchema = z.object(
{
firstName: z.string(),
Expand Down Expand Up @@ -56,10 +91,39 @@ export type PersonalDataAndDocumentsKyc = z.infer<
typeof personalDataAndDocumentsKycSchema
>

export const personalDataAndDocumentsDetailedKycSchema =
personalDataAndDocumentsKycSchema
.omit({ identificationDocument: true })
.and(
z.object({
email: z.string().regex(EMAIL_REGEX),
identificationDocumentFront: z.string(),
}),
)
.and(
z
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assume the linter did this due to the next line's length, but weird to have a line containing only z here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh i hate this but yes it's the linter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

futzed around for a while trying to get the z some company on that line, to no avail

.object({
identificationDocumentType: identificationDocumentTypeWithBackSchema,
identificationDocumentBack: z.string(),
})
.or(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this or is "selected" by the parser (by identificationDocumentBack being omitted from the object), does the type of identificationDocumentType described here (identificationDocumentTypeWithoutBackSchema) override the more general one set above?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is, if identificationDocumentBack is missing, does this setup cause Zod to enforce that identificationDocumentType be of type identificationDocumentTypeWithoutBackSchema?

Copy link
Contributor Author

@cajubelt cajubelt Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, but to avoid having to reason about this I just dropped it. seems simpler.

z.object({
identificationDocumentType:
identificationDocumentTypeWithoutBackSchema,
}),
),
)

export type PersonalDataAndDocumentsDetailedKyc = z.infer<
typeof personalDataAndDocumentsDetailedKycSchema
>

export const kycSchemasSchema = z.object(
{
[kycSchemaSchema.enum.PersonalDataAndDocuments]:
personalDataAndDocumentsKycSchema,
[kycSchemaSchema.enum.PersonalDataAndDocumentsDetailed]:
personalDataAndDocumentsDetailedKycSchema,
},
{ description: 'kycSchemasSchema' },
)
Expand Down