samchon / typia

Super-fast/easy runtime validations and serializations through transformation

Home Page:https://typia.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stringify arrays in validation reports

jmroon opened this issue · comments

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]
    ]
  }
]

Needs reproducible playground link or repo.

Needs reproducible playground link or repo.

I can get that to you very early next week!

Close due to too much time no reproduciple repo updating.