typestack/class-sanitizer được TopGit xếp vào nhóm dự án frontend, với 100 sao trên GitHub, viết chủ yếu bằng TypeScript. Decorator based class property sanitation in Typescript.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Decorator based class property sanitation in Typescript powered by validator.js.
DEPRECATION NOTICE:
This library is considered to be deprecated and won't be updated anymore. Please use the class-transformer and/or class-validator libraries instead.
Installation
npm install class-sanitizer --save
Usage
To start using the library simply create some classes and add some sanitization decorators to the properties. When calling
sanitize(instance) the library will automatically apply the rules defined in the decorators to the properties and update
the value of every marked property respectively.
NOTE:
Every sanitization decorator is property decorator meaning it cannot be placed on parameters or class definitions.
import { sanitize, Trim } from 'class-sanitizer';
class TestClass {
@Trim()
label: string;
constructor(label: string) {
this.label = label;
}
}
const instance = new TestClass(' text-with-spaces-on-both-end ');
sanitize(instance);
// -> the label property is trimmed now
// -> { label: 'text-with-spaces-on-both-end' }
Validating arrays
Every decorator expects a SanitationOptions object. When the each property is set to true
the array will be iterated and the decorator will be applied to every element of the array.
import { sanitize, Trim } from 'class-sanitizer';
class TestClass {
@Trim(undefined, { each: true })
labels: string[];
constructor(labels: string[]) {
this.labels = labels;
}
}
const instance = new TestClass([' labelA ', ' labelB', 'labelC ']);
sanitize(instance);
// -> Every value is trimmed in instance.labels now.
// -> { labels: ['labelA', 'labelB', 'labelC']}
Inheritance
Class inheritance is supported, every decorator defined on the base-class will
be applied to the property with same name on the descendant class if the property exists.
Note: Only one level of inheritance is supported! So if you have ClassA inherit ClassB which inherits ClassC the
decorators from ClassC won't be applied to ClassA when sanitizing.
import { sanitize, Trim } from 'class-sanitizer';
class BaseClass {
@Trim()
baseText: string;
}
class DescendantClass extends BaseClass {
@Trim()
descendantText: string;
}
const instance = new DescendantClass();
instance.baseText = ' text ';
instance.descendantText = ' text ';
sanitize(instance);
// -> Both value is trimmed now.
// -> { baseText: 'text', descendantText: 'text' }
Sanitizing nested values with @SanitizeNested() decorator
The @SanitizeNested property can be used to instruct the library to lookup the sanitization rules
for the class instance found on the marked property and sanitize it.
import { sanitize, Trim, SanitizeNested } from 'class-sanitizer';
class InnerTestClass {
@Trim()
text: string;
constructor(text: string) {
this.text = text;
}
}
class TestClass {
@SanitizeNested({ each: true })
children: InnerTestClass[];
@SanitizeNested({ each: false })
child: InnerTestClass;
}
const instance = new TestClass();
const innerA = new InnerTestClass(' innerA ');
const innerB = new InnerTestClass(' innerB ');
const innerC = new InnerTestClass(' innerC ');
instance.children = [innerA, innerB];
instance.child = innerC;
sanitize(instance);
// -> Both values in the array on `children` property and value on `child` property is sanitized.
// -> { children: [ { text: 'innerA' }, { text: 'innerB' }], child: { 'innerC' }}
Custom sanitation classes
The @SanitizerConstraint( decorator can be used to define custom sanitization logic. Creating a custom sanitization class requires the following steps:
Create a class which implements the CustomSanitizer interface and decorate the class with the @SanitizerConstraint() decorator.
import { CustomSanitizer, SanitizerConstraint } from 'class-sanitizer';
import { Container } from 'typedi';
@SanitizerConstraint()
export class LetterReplacer implements CustomSanitizer {
/** If you use TypeDI, you can inject services to properties with `Container.get` function. */
someInjectedService = Container.get(SomeClass);
/**
* This function will be called during sanitization.
* 1, It must be a sync function
* 2, It must return the transformed value.
*/
sanitize(text: string): string {
return text.replace(/o/g, 'w');
}
}
Then you can use your new sanitation constraint in your class:
import { Sanitize } from 'class-sanitizer';
import { LetterReplacer } from './LetterReplacer';
export class Post {
@Sanitize(LetterReplacer)
title: string;
}
Now you can use sanitizer as usual:
import { sanitize } from 'class-sanitizer';
sanitize(post);
Manual sanitation
There are several method exist in the Sanitizer that allows to perform non-decorator based sanitation:
import Sanitizer from 'class-sanitizer';
Sanitizer.trim(` Let's trim this! `);
Sanitization decorators
The following property decorators are available.
Decorator
Description
@Blacklist(chars: string)
Removes all characters that appear in the blacklist.
@Whitelist(chars: string)
Removes all characters that don't appear in the whitelist.
@Trim(chars?: string)
Trims characters (whitespace by default) from both sides of the input. You can specify chars that should be trimmed.
@Ltrim(chars?: string)
Trims characters from the left-side of the input.
@Rtrim(chars?: string)
Trims characters from the right-side of the input.
@Escape()
Replaces <, >, &, ', " and / with HTML entities.
@NormalizeEmail(lowercase?: boolean)
Normalizes an email address.
@StripLow(keepNewLines?: boolean)
Removes characters with a numerical value < 32 and 127, mostly control characters.
@ToBoolean(isStrict?: boolean)
Converts the input to a boolean. Everything except for '0', 'false' and '' returns true. In strict mode only '1' and 'true' return true.
@ToDate()
Converts the input to a date, or null if the input is not a date.
@ToFloat()
Converts the input to a float, or NaN if the input is not an integer.
@ToInt(radix?: number)
Converts the input to an integer, or NaN if the input is not an integer.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/typestack/class-sanitizer là nguồn chính thức.
typestack/class-sanitizer có những chủ đề gì?
GitHub topics của typestack/class-sanitizer: "sanitizer", "typescript". TopGit xếp repo vào nhóm Frontend.
typestack/class-sanitizer có phải mã nguồn mở không?
Có — typestack/class-sanitizer phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/typestack/class-sanitizer.
typestack/class-sanitizer có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho typestack/class-sanitizer. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
typestack/class-sanitizer còn đang phát triển không?
Commit gần nhất trên typestack/class-sanitizer là 6.0 năm trước (theo timestamp GitHub). Repo có 18 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
typestack/class-sanitizer dùng license gì?
typestack/class-sanitizer phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về class-sanitizer?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về class-sanitizer.