fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
5.6k stars 169 forks source link

Support for String normalization #691

Closed N0tExisting closed 3 days ago

N0tExisting commented 1 week ago

It would be nice if there was built-in support for String.prototype.normalize as a built in transformer.

This would be usefull to avoid wierd issues with strings:

const name1 = '\u0041\u006d\u00e9\u006c\u0069\u0065';
const name2 = '\u0041\u006d\u0065\u0301\u006c\u0069\u0065';

console.log(`${name1}, ${name2}`); // "Amélie, Amélie"
console.log(name1 === name2); // false
console.log(name1.length === name2.length); // false

const name1NFC = name1.normalize('NFC');
const name2NFC = name2.normalize('NFC');

console.log(`${name1NFC}, ${name2NFC}`); // "Amélie, Amélie"
console.log(name1NFC === name2NFC); // true
console.log(name1NFC.length === name2NFC.length); // true

For options and more detail see the MDN documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize

fabian-hiller commented 1 week ago

Great idea! I plan to add this in our next release. Until then, you can use transform:

import * form 'valibot';

const Schema = v.pipe(
  v.string(),
  v.transform((input) => input.normalize('NFC'))
);
fabian-hiller commented 3 days ago

v0.36.0 is available