@palmares/schemas 0.1.4 → 0.1.6

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.
@@ -0,0 +1,29 @@
1
+ import { Response } from '@palmares/server';
2
+ /**
3
+ * Validates the request body and returns a response automatically, don't need to do anything else.
4
+ */ export default function schemaHandler(input, output) {
5
+ return async (request)=>{
6
+ const data = await request.json();
7
+ const validatedData = await input.validate(data, {
8
+ request
9
+ });
10
+ if (validatedData.isValid) {
11
+ const savedData = await validatedData.save();
12
+ const status = request.method === 'POST' ? 201 : 200;
13
+ if (output) return Response.json(await output.data(savedData), {
14
+ status: status
15
+ });
16
+ return Response.json(savedData, {
17
+ status,
18
+ headers: {
19
+ 'content-type': 'application/json'
20
+ }
21
+ });
22
+ }
23
+ return Response.json({
24
+ errors: validatedData.errors
25
+ }, {
26
+ status: 400
27
+ });
28
+ };
29
+ }
@@ -481,8 +481,10 @@ export default class Schema {
481
481
  *
482
482
  * @returns An object with the property isValid, if the value is valid, the function `save` will be present.
483
483
  * If the value is invalid, the property errors will be present.
484
- */ async validate(value) {
485
- const { errors, parsed } = await this.__parse(value, [], {});
484
+ */ async validate(value, context) {
485
+ const { errors, parsed } = await this.__parse(value, [], {
486
+ context
487
+ });
486
488
  // eslint-disable-next-line ts/no-unnecessary-condition
487
489
  if ((errors || []).length <= 0) return {
488
490
  isValid: false,
@@ -490,7 +492,7 @@ export default class Schema {
490
492
  };
491
493
  return {
492
494
  isValid: true,
493
- save: async ()=>this._save.bind(this)(parsed)
495
+ save: async ()=>this._save.bind(this)(parsed, context)
494
496
  };
495
497
  }
496
498
  /**
@@ -500,9 +502,9 @@ export default class Schema {
500
502
  * @param value - The value to be saved.
501
503
  *
502
504
  * @returns The value to representation.
503
- */ async _save(value) {
505
+ */ async _save(value, context) {
504
506
  if (this.__saveCallback) {
505
- const result = await this.__saveCallback(value);
507
+ const result = await this.__saveCallback(value, context);
506
508
  return this.data(result);
507
509
  }
508
510
  return this.data(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@palmares/schemas",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "This defines a default schema definition for validation of data, it abstract popular schema validation libraries like zod, yup, valibot and others\"",
5
5
  "main": "./dist/cjs/src/index.js",
6
6
  "module": "./dist/esm/src/index.js",
@@ -32,7 +32,7 @@
32
32
  "homepage": "https://github.com/palmaresHQ/palmares#readme",
33
33
  "dependencies": {
34
34
  "@palmares/core": "0.1.4",
35
- "@palmares/databases": "0.1.4",
35
+ "@palmares/databases": "0.1.6",
36
36
  "@palmares/server": "0.1.4"
37
37
  },
38
38
  "scripts": {
package/src/index.ts CHANGED
@@ -40,6 +40,7 @@ export {
40
40
  };
41
41
  export { schema, number, object, union, string, array, datetime, boolean };
42
42
  export { default as compile } from './compile';
43
+ export { default as schemaHandler } from './middleware';
43
44
 
44
45
  export { modelSchema };
45
46