@theshelf/validation 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,34 +9,37 @@ The validation package provides a universal interaction layer with an actual dat
9
9
  npm install @theshelf/validation
10
10
  ```
11
11
 
12
- ## Implementations
12
+ ## Drivers
13
13
 
14
- Currently, there is only one implementation:
14
+ Currently, there is only one driver available:
15
15
 
16
- * **Zod** - implementation for the currently popular Zod library.
17
-
18
- ## Configuration
19
-
20
- The used implementation needs to be configured in the `.env` file.
21
-
22
- ```env
23
- VALIDATION_IMPLEMENTATION="zod"
24
- ```
16
+ * **Zod** - driver for the currently popular Zod library.
25
17
 
26
18
  ## How to use
27
19
 
28
- An instance of the configured validator implementation can be imported for performing validation operations.
20
+ The instance of the validator needs to be imported and one of the drivers must be set.
29
21
 
30
22
  ```ts
31
- import validator from '@theshelf/validation';
23
+ import validator, { Zod as SelectedDriver } from '@theshelf/validation';
24
+
25
+ // Set the driver before performing any operation (the Zod driver is used by default)
26
+ validator.driver = new SelectedDriver(/* configuration */);
32
27
 
33
28
  // Perform operations with the validator instance
34
29
  ```
35
30
 
31
+ ### Configuration
32
+
33
+ The validator instance does not have any configuration options.
34
+
35
+ #### Zod driver
36
+
37
+ No configuration options.
38
+
36
39
  ### Operations
37
40
 
38
41
  ```ts
39
- import validator, { ValidationSchema, ValidationResult } from '@theshelf/validation';
42
+ import { ValidationSchema, ValidationResult } from '@theshelf/validation';
40
43
 
41
44
  const data = {
42
45
  name: 'John Doe',
@@ -0,0 +1,8 @@
1
+ import type { Driver } from './definitions/interfaces.js';
2
+ import type { ValidationSchema } from './definitions/types.js';
3
+ import type ValidationResult from './definitions/ValidationResult.js';
4
+ export default class Validator implements Driver {
5
+ #private;
6
+ constructor(driver: Driver);
7
+ validate(data: unknown, schema: ValidationSchema): ValidationResult;
8
+ }
@@ -0,0 +1,9 @@
1
+ export default class Validator {
2
+ #driver;
3
+ constructor(driver) {
4
+ this.#driver = driver;
5
+ }
6
+ validate(data, schema) {
7
+ return this.#driver.validate(data, schema);
8
+ }
9
+ }
@@ -1,14 +1,14 @@
1
1
  declare const FieldTypes: {
2
- STRING: string;
3
- NUMBER: string;
4
- BOOLEAN: string;
5
- DATE: string;
6
- DATETIME: string;
7
- UUID: string;
8
- EMAIL: string;
9
- ARRAY: string;
10
- URL: string;
11
- ENUM: string;
2
+ readonly STRING: "string";
3
+ readonly NUMBER: "number";
4
+ readonly BOOLEAN: "boolean";
5
+ readonly DATE: "date";
6
+ readonly DATETIME: "datetime";
7
+ readonly UUID: "uuid";
8
+ readonly EMAIL: "email";
9
+ readonly ARRAY: "array";
10
+ readonly URL: "url";
11
+ readonly ENUM: "enum";
12
12
  };
13
13
  declare const MAX_EMAIL_LENGTH = 320;
14
14
  declare const MAX_URL_LENGTH = 2083;
@@ -10,7 +10,6 @@ const FieldTypes = {
10
10
  URL: 'url',
11
11
  ENUM: 'enum'
12
12
  };
13
- Object.freeze(FieldTypes);
14
13
  const MAX_EMAIL_LENGTH = 320;
15
14
  const MAX_URL_LENGTH = 2083;
16
15
  export { FieldTypes, MAX_EMAIL_LENGTH, MAX_URL_LENGTH };
@@ -1,5 +1,5 @@
1
1
  import type ValidationResult from './ValidationResult.js';
2
2
  import type { ValidationSchema } from './types.js';
3
- export interface Validator {
3
+ export interface Driver {
4
4
  validate(data: unknown, schema: ValidationSchema): ValidationResult;
5
5
  }
@@ -0,0 +1,8 @@
1
+ import ValidationResult from '../definitions/ValidationResult.js';
2
+ import type { Driver } from '../definitions/interfaces.js';
3
+ import type { ValidationSchema } from '../definitions/types.js';
4
+ export default class Zod implements Driver {
5
+ #private;
6
+ constructor();
7
+ validate(data: unknown, schema: ValidationSchema): ValidationResult;
8
+ }
@@ -1,7 +1,7 @@
1
1
  import { z } from 'zod';
2
- import ValidationResult from '../../definitions/ValidationResult.js';
3
- import { FieldTypes, MAX_EMAIL_LENGTH, MAX_URL_LENGTH } from '../../definitions/constants.js';
4
- import UnknownValidator from '../../errors/UnknownValidator.js';
2
+ import ValidationResult from '../definitions/ValidationResult.js';
3
+ import { FieldTypes, MAX_EMAIL_LENGTH, MAX_URL_LENGTH } from '../definitions/constants.js';
4
+ import UnknownValidator from '../errors/UnknownValidator.js';
5
5
  // Zod is so type heavy that we've chosen for inferred types to be used.
6
6
  // This is a trade-off between readability and verbosity.
7
7
  export default class Zod {
@@ -39,7 +39,7 @@ export default class Zod {
39
39
  for (const [key, validation] of Object.entries(schema)) {
40
40
  if (key === 'message')
41
41
  continue;
42
- const validator = this.#validations.get(key.toLocaleLowerCase());
42
+ const validator = this.#validations.get(key.toLowerCase());
43
43
  if (validator === undefined) {
44
44
  throw new UnknownValidator(key);
45
45
  }
@@ -1,5 +1,9 @@
1
1
  export default class ValidationError extends Error {
2
2
  constructor(message) {
3
3
  super(message ?? 'Validation error');
4
+ this.name = this.constructor.name;
5
+ if (Error.captureStackTrace) {
6
+ Error.captureStackTrace(this, this.constructor);
7
+ }
4
8
  }
5
9
  }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './definitions/constants.js';
2
- export * from './definitions/types.js';
3
- export { default as UnknownImplementation } from './errors/UnknownImplementation.js';
2
+ export type * from './definitions/interfaces.js';
3
+ export type * from './definitions/types.js';
4
4
  export { default as UnknownValidator } from './errors/UnknownValidator.js';
5
5
  export { default as ValidationError } from './errors/ValidationError.js';
6
- export { default } from './implementation.js';
6
+ export { default as ZodDriver } from './drivers/Zod.js';
7
+ export { default } from './Validator.js';
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  export * from './definitions/constants.js';
2
- export * from './definitions/types.js';
3
- export { default as UnknownImplementation } from './errors/UnknownImplementation.js';
4
2
  export { default as UnknownValidator } from './errors/UnknownValidator.js';
5
3
  export { default as ValidationError } from './errors/ValidationError.js';
6
- export { default } from './implementation.js';
4
+ export { default as ZodDriver } from './drivers/Zod.js';
5
+ export { default } from './Validator.js';
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@theshelf/validation",
3
3
  "private": false,
4
- "version": "0.1.0",
4
+ "version": "0.2.1",
5
5
  "type": "module",
6
6
  "repository": {
7
- "url": "https://github.com/MaskingTechnology/theshelf"
7
+ "url": "git+https://github.com/MaskingTechnology/theshelf.git"
8
8
  },
9
9
  "scripts": {
10
10
  "build": "tsc",
@@ -1,4 +0,0 @@
1
- import ValidationError from './ValidationError.js';
2
- export default class UnknownImplementation extends ValidationError {
3
- constructor(name: string);
4
- }
@@ -1,6 +0,0 @@
1
- import ValidationError from './ValidationError.js';
2
- export default class UnknownImplementation extends ValidationError {
3
- constructor(name) {
4
- super(`Unknown validation implementation: ${name}`);
5
- }
6
- }
@@ -1,3 +0,0 @@
1
- import type { Validator } from './definitions/interfaces.js';
2
- declare const _default: Validator;
3
- export default _default;
@@ -1,12 +0,0 @@
1
- import UnknownImplementation from './errors/UnknownImplementation.js';
2
- import createZod from './implementations/zod/create.js';
3
- const implementations = new Map([
4
- ['zod', createZod]
5
- ]);
6
- const DEFAULT_VALIDATION_IMPLEMENTATION = 'zod';
7
- const implementationName = process.env.VALIDATION_IMPLEMENTATION ?? DEFAULT_VALIDATION_IMPLEMENTATION;
8
- const creator = implementations.get(implementationName.toLowerCase());
9
- if (creator === undefined) {
10
- throw new UnknownImplementation(implementationName);
11
- }
12
- export default creator();
@@ -1,8 +0,0 @@
1
- import ValidationResult from '../../definitions/ValidationResult.js';
2
- import type { Validator } from '../../definitions/interfaces.js';
3
- import type { ValidationSchema } from '../../definitions/types.js';
4
- export default class Zod implements Validator {
5
- #private;
6
- constructor();
7
- validate(data: unknown, schema: ValidationSchema): ValidationResult;
8
- }
@@ -1,2 +0,0 @@
1
- import Zod from './Zod.js';
2
- export default function create(): Zod;
@@ -1,4 +0,0 @@
1
- import Zod from './Zod.js';
2
- export default function create() {
3
- return new Zod();
4
- }