mongoose-data-sanitizer
v1.0.1
Published
A sanitizer plugin that makes sure the input is in UTF-8 format
Maintainers
Readme
mongoose-data-sanitizer
A simple Mongoose plugin that automatically sanitizes your data before saving or updating.
It ensures your documents are normalized to NFC and removes invalid control characters that might cause issues with MongoDB or string handling.
📦 Installation
npm install mongoose-data-sanitizerWhat It Does
Converts strings to NFC normalized form
Removes invalid binary/control characters
Works on:
save()
updateOne()
findOneAndUpdate()
updateMany()
- Supports nested objects and arrays
Usage
import mongoose, { Schema, model } from "mongoose";
import { sanitizerPlugin } from "mongoose-data-sanitizer";
// Define your schema
const userSchema = new Schema({
name: String,
bio: String,
tags: [String],
});
// Attach the sanitizer plugin
userSchema.plugin(sanitizerPlugin);
const User = model("User", userSchema);
async function run() {
await mongoose.connect("mongodb://localhost:27017/test");
const user = new User({
name: "Jo\u0301hn", // combining accent
bio: "Some bad char \u0008 here",
tags: ["hel\u200Blo", "wo\u0000rld"],
});
await user.save();
console.log(await User.findOne({ _id: user._id }));
await mongoose.disconnect();
}