Skip to content

Commit

Permalink
feat: enable defining custom error reporter at all layers
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 12, 2023
1 parent bc38c04 commit 0138b5f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export type ValidationOptions = {
* Validation errors are reported directly to an error reporter. The reporter
* can decide how to format and output errors.
*/
errorReporter?: ErrorReporterContract
errorReporter?: () => ErrorReporterContract
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/vine/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ import { SimpleMessagesProvider } from '../messages_provider/simple_messages_pro

import { VineValidator } from './validator.js'
import { fields, messages } from '../defaults.js'
import type { Infer, MessagesProviderContact, SchemaTypes, ValidationOptions } from '../types.js'
import type {
Infer,
SchemaTypes,
ValidationOptions,
ErrorReporterContract,
MessagesProviderContact,
} from '../types.js'
import { SimpleErrorReporter } from '../reporters/simple_error_reporter.js'

/**
* Validate user input with type-safety using a pre-compiled schema.
Expand All @@ -25,6 +32,11 @@ export class Vine extends SchemaBuilder {
*/
messagesProvider: MessagesProviderContact = new SimpleMessagesProvider(messages, fields)

/**
* Error reporter to use on the validator
*/
errorReporter: () => ErrorReporterContract = () => new SimpleErrorReporter()

/**
* Control whether or not to convert empty strings to null
*/
Expand Down Expand Up @@ -53,6 +65,7 @@ export class Vine extends SchemaBuilder {
return new VineValidator(schema, {
convertEmptyStringsToNull: this.convertEmptyStringsToNull,
messagesProvider: this.messagesProvider,
errorReporter: this.errorReporter,
})
}

Expand Down
22 changes: 17 additions & 5 deletions src/vine/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import { Compiler, refsBuilder } from '@vinejs/compiler'
import type { MessagesProviderContact, Refs } from '@vinejs/compiler/types'

import { OTYPE, PARSE } from '../symbols.js'
import { messages } from '../defaults.js'
import { SimpleErrorReporter } from '../reporters/simple_error_reporter.js'
import type { Infer, SchemaTypes, ValidationOptions } from '../types.js'
import { OTYPE, PARSE } from '../symbols.js'
import type { ErrorReporterContract, Infer, SchemaTypes, ValidationOptions } from '../types.js'

/**
* Error messages to share with the compiler
Expand All @@ -39,6 +38,11 @@ export class VineValidator<Schema extends SchemaTypes> {
*/
messagesProvider: MessagesProviderContact

/**
* Error reporter to use on the validator
*/
errorReporter: () => ErrorReporterContract

/**
* Parses schema to compiler nodes.
*/
Expand Down Expand Up @@ -68,6 +72,7 @@ export class VineValidator<Schema extends SchemaTypes> {
options: {
convertEmptyStringsToNull: boolean
messagesProvider: MessagesProviderContact
errorReporter: () => ErrorReporterContract
}
) {
const { compilerNode, refs } = this.#parse(schema)
Expand All @@ -78,6 +83,7 @@ export class VineValidator<Schema extends SchemaTypes> {
messages: COMPILER_ERROR_MESSAGES,
}).compile()

this.errorReporter = options.errorReporter
this.messagesProvider = options.messagesProvider
}

Expand All @@ -97,8 +103,14 @@ export class VineValidator<Schema extends SchemaTypes> {
* ```
*/
validate(data: any, options?: ValidationOptions): Promise<Infer<Schema>> {
const errorReporter = options?.errorReporter || new SimpleErrorReporter()
const errorReporter = options?.errorReporter || this.errorReporter
const messagesProvider = options?.messagesProvider || this.messagesProvider
return this.#validateFn(data, options?.meta || {}, this.#refs, messagesProvider, errorReporter)
return this.#validateFn(
data,
options?.meta || {},
this.#refs,
messagesProvider,
errorReporter()
)
}
}

0 comments on commit 0138b5f

Please sign in to comment.