Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,16 @@ import {
ComposedValSan
} from 'valsan';

class UsernameValSan extends ComposedValSan<string, string> {
public constructor() {
super([
new TrimSanitizer(),
new LengthValidator({ minLength: 5, maxLength: 10 }),
new LowercaseSanitizer(),
]);
}
}
const usernameValSan = new ComposedValSan<string, string>([
new TrimSanitizer(),
new LengthValidator({ minLength: 5, maxLength: 10 }),
new LowercaseSanitizer(),
]);

// Create an object sanitizer
const sanitizer = new ObjectSanitizer({
username: new UsernameValSan(),
username: usernameValSan,
optionalUsername: usernameValSan.copy({ isOptional: true }),
email: new EmailValidator(),
});

Expand Down
22 changes: 22 additions & 0 deletions docs/using-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,25 @@ You can use options to:
- Pass dependencies (e.g. database)

All options are optional and have sensible defaults. See validator docs for details.

## Copying

You can copy an existing validator or sanitizer and override its options or rules using the `.copy()` method. This is handy when you want a slightly different behavior (for example making a validator optional) without recreating the instance.

Example:

```typescript
import { ComposedValSan, MinLengthValidator } from 'valsan';

const minLen = new ComposedValSan([
MinLengthValidator({ minLength: 5 })
]);

// Copy to make optional
const optionalMinLen = minLen.copy({ isOptional: true });

await minLen.run('hi'); // fails
await optionalMinLen.run(undefined); // passes because it's now optional
```

`.copy()` returns a new instance with the provided overrides merged into the original options; the original instance is unchanged.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "valsan",
"version": "2.2.0",
"version": "2.2.1",
"description": "Validation and sanitization library for TypeScript",
"private": "true",
"typescript-template": {
Expand Down
9 changes: 7 additions & 2 deletions src/primitives/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ export * from './bool';
// Date-time primitives
export * from './date-time';

export * from './network';

// Number primitives
export * from './number';

// Person primitives
export * from './person';

// String primitives
export * from './string';

// Number primitives
export * from './number';
// Utility primitives
export * from './utility';
2 changes: 1 addition & 1 deletion test/spec/primitives/network/ip-address-valsan.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line max-len
import { IpAddressValSan } from '../../../../src/primitives/network/ip-address-valsan';
import { IpAddressValSan } from '../../../../src/primitives';

describe('IpAddressValSan', () => {
const valSan = new IpAddressValSan();
Expand Down
2 changes: 1 addition & 1 deletion test/spec/primitives/utility/enum-validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line max-len
import { EnumValidator } from '../../../../src/primitives/utility/enum-validator';
import { EnumValidator } from '../../../../src/primitives/utility';

describe('EnumValidator', () => {
it('should pass for allowed values', async () => {
Expand Down