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

Stringify arrays in validation reports #1057

Open
jmroon opened this issue May 17, 2024 · 2 comments
Open

Stringify arrays in validation reports #1057

jmroon opened this issue May 17, 2024 · 2 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@jmroon
Copy link

jmroon commented May 17, 2024

Hi Samchon! First off, thank you for making this great library.

I am currently iterating over my collections and am validating all items in my database. With typia, this has been made very easy and it's easy to find instances where my data and types have diverged.

However, when it comes to nested arrays, the validation reporting is not terribly helpful, as it does not appear to serialize objects. Would it be possible to serialize the objects on validation printouts if not primitive? I suppose this may be problematic for recursive data structures, but perhaps an opt-in option to allow doing so would be helpful. Or perhaps object serialization is already handled and this is simple an issue with nested arrays.

sample script doing the validation:

import { Db, MongoClient } from 'mongodb'
import { createValidate, IValidation } from 'typia'
import { Element } from '../src/types/element'
import { TrainingAudience, TrainingEvaluation, TrainingGoal } from '../src/types/training'

const collectionValidators = {
  'training.audiences': createValidate<TrainingAudience>(),
  'training.goals': createValidate<TrainingGoal>(),
  'training.evaluations': createValidate<TrainingEvaluation>(),
  elements: createValidate<Element>(),
}

async function main() {
  console.log('Attempting to connect to DB')
  const client = await MongoClient.connect(
    'mongodb://admin:password@localhost:27017',
  )

  try {
    const db = client.db('database')

    for (const collectionValidator of Object.entries(collectionValidators)) {
      await validateCollection(db, ...collectionValidator)
    }
  } catch (error) {
    console.error(`Error: ${error}`)
  } finally {
    await client.close()
    console.log('DB connection closed')
  }
}

async function validateCollection(
  db: Db,
  collectionName: string,
  validate: (input: unknown) => IValidation,
) {
  console.log(`Validating ${collectionName}`)
  const cursor = db.collection(collectionName).find({})
  for await (const doc of cursor) {
    const res = validate(doc)
    if (!res.success) {
      console.error(`Validation failed for ${doc.metadata.uuid}`)
      console.log(res.errors)
    }
  }
}

main()

Output from validation errors:

Validation failed for 88b8efc0-3721-4484-8d46-62c3b18750e6
[
  {
    path: '$input.data[11].content',
    expected: '(Array<DataTextItem> | Array<DataImageItem>)',
    value: [
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array], [Array], [Array],
      [Array], [Array], [Array]
    ]
  }
]

@samchon
Copy link
Owner

samchon commented May 18, 2024

Needs reproducible playground link or repo.

@samchon samchon self-assigned this May 18, 2024
@samchon samchon added the help wanted Extra attention is needed label May 18, 2024
@jmroon
Copy link
Author

jmroon commented May 18, 2024

Needs reproducible playground link or repo.

I can get that to you very early next week!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants