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

FEATURE: better validation error reporting #4

Open
wants to merge 1 commit into
base: mongo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ yarn add -D prisma-mongo-json-schema-generator
**2. Apply the schema to database**

```shell
PRISMA_SCHEMA_FILE=prisma/prisma.schema MONGO_URI=mongodb://localhost:27017/your-database yarn prisma-mongo-json-schema-generator-apply
PRISMA_SCHEMA_FILE=prisma/schema.prisma MONGO_URI=mongodb://localhost:27017/your-database yarn prisma-mongo-json-schema-generator-apply
```

If you are getting `AuthenticationFailed` errors, make sure that you are specifying the correct auth database in the connection URI by using the `authSource` query parameter (ex: `authSource=admin`).
Expand All @@ -45,7 +45,7 @@ The package is configurable via environment variables:
| Env name | Default value |
|--|--|
| MONGO_URI | required |
| PRISMA_SCHEMA_FILE | `prisma/prisma.schema` |
| PRISMA_SCHEMA_FILE | `prisma/schema.prisma` |
| VALIDATION_LEVEL | `strict` |
| VALIDATION_ACTION | `error` |

Expand All @@ -61,5 +61,5 @@ model SomeModel {
**3. Validate collections according to the schema**

```shell
PRISMA_SCHEMA_FILE=prisma/prisma.schema MONGO_URI=mongodb://localhost:27017/your-database yarn prisma-mongo-json-schema-generator-verify
PRISMA_SCHEMA_FILE=prisma/schema.prisma MONGO_URI=mongodb://localhost:27017/your-databse yarn prisma-mongo-json-schema-generator-verify
```
48 changes: 44 additions & 4 deletions src/scripts/verifyMongoSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,50 @@ export const verifyMongoSchema = async (schema: {
],
})
if (firstFailedDocument) {
console.info(
`❌ Collection ${collectionName} failed validation, first failed document is: `,
firstFailedDocument,
)
try {
// In order to provide some better debug information, we try to update the first failed document in hopes of getting `schemaRulesNotSatisfied` information
await database.collection(collectionName).updateOne(
{ _id: firstFailedDocument._id },
{
$set: {
___schmaValidator: Math.random(),
},
},
)
// We shouldn't have gotten to this point, the previous update should have thrown due to failed $jsonSchema constraints.
// If it didn't, it probably means that we forgot to `apply` the schema or that the validation action is not "error"
console.warn(
'Not able to provide the full debug info, make sure that you have applied the schema with `VALIDATION_LEVEL=strict VALIDATION_ACTION=error`',
)
console.info(
`❌ Collection ${collectionName} failed validation, first failed document is: `,
firstFailedDocument,
)
// Cleanup
await database.collection(collectionName).updateOne(
{ _id: firstFailedDocument._id },
{
$unset: {
___schmaValidator: '',
},
},
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
console.info(
`❌ Collection ${collectionName} failed validation, first failed document is: `,
firstFailedDocument,
)
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (e?.errInfo?.details?.schemaRulesNotSatisfied) {
console.log('Reason for failure:')
console.dir(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
e.errInfo.details.schemaRulesNotSatisfied,
{ depth: null },
)
}
}
} else {
console.info(
`✅ Collection ${collectionName} passed validation`,
Expand Down