DOCUMENTATION
SCHEMA
Validation Methods
.buffer

Schema - Buffer

The .buffer method is used to validate whether a given value is of type Buffer. It ensures that the input is an instance of the Buffer class, which is typically used to handle raw binary data, such as when working with files, streams, or binary communication.

This method is essential for enforcing strict type validation where only Buffer values are acceptable, making it useful for scenarios like validating binary data, file uploads, or raw byte streams.


Content


Quick Start

Below is an example of how to use .buffer to validate whether a value is a valid Buffer:

import { schema } from 'vkrun'
 
const schemaValidation = schema().buffer()
 
const validateA = schemaValidation.validate(Buffer.from('test')) // Valid Buffer
const validateB = schemaValidation.validate('test') // Invalid, string is not a Buffer
 
console.log(validateA) // true
console.log(validateB) // false

Error Messages

The .buffer method provides robust error messages to help identify issues during validation. These messages can be used as is or customized for specific use cases.

Default Error Message

If no custom error message is provided, the .buffer method generates the following default message:

"[valueName] must be a Buffer type!"

This message dynamically replaces [valueName] with the name of the variable being validated, providing clarity about the source of the error.

Example

import { schema } from 'vkrun'
 
const value = 'test'
 
const schemaValidation = schema().buffer()
 
const defaultResult = schemaValidation.parse(value, 'buffer_value')

Throw:

buffer_value must be a buffer type!

Custom Error Messages

To make error messages more descriptive or tailored to specific use cases, you can define custom messages using keywords like [value] and [valueName]. These keywords are replaced dynamically with the actual value and the name of the variable being validated.

Supported Keywords

  • [value]: The actual value being validated.
  • [valueName]: The name of the value being validated.

Example

import { schema } from 'vkrun'
 
const value = 'test'
 
const schemaValidation = schema().buffer({
	message: '[valueName] must be of type Buffer, but received [value]!',
})
 
const customResult = schemaValidation.parse(value, 'buffer_value')

Throw:

buffer_value must be of type Buffer, but received test!
Copyright © 2024 - 2025 MIT by Mario Elvio