@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.
- package/CHANGELOG.md +15 -0
- package/dist/cjs/src/index.js +4 -0
- package/dist/cjs/src/middleware.js +215 -0
- package/dist/cjs/src/schema/schema.js +7 -5
- package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
- package/dist/cjs/types/domain.d.ts +16 -2
- package/dist/cjs/types/domain.d.ts.map +1 -1
- package/dist/cjs/types/index.d.ts +1 -0
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/middleware.d.ts +34 -0
- package/dist/cjs/types/middleware.d.ts.map +1 -0
- package/dist/cjs/types/schema/array.d.ts +1 -1
- package/dist/cjs/types/schema/array.d.ts.map +1 -1
- package/dist/cjs/types/schema/boolean.d.ts +1 -1
- package/dist/cjs/types/schema/boolean.d.ts.map +1 -1
- package/dist/cjs/types/schema/datetime.d.ts +1 -1
- package/dist/cjs/types/schema/datetime.d.ts.map +1 -1
- package/dist/cjs/types/schema/number.d.ts +1 -1
- package/dist/cjs/types/schema/number.d.ts.map +1 -1
- package/dist/cjs/types/schema/object.d.ts +1 -1
- package/dist/cjs/types/schema/object.d.ts.map +1 -1
- package/dist/cjs/types/schema/schema.d.ts +5 -4
- package/dist/cjs/types/schema/schema.d.ts.map +1 -1
- package/dist/cjs/types/schema/string.d.ts +1 -1
- package/dist/cjs/types/schema/string.d.ts.map +1 -1
- package/dist/cjs/types/schema/union.d.ts +1 -1
- package/dist/cjs/types/schema/union.d.ts.map +1 -1
- package/dist/esm/src/index.js +1 -0
- package/dist/esm/src/middleware.js +29 -0
- package/dist/esm/src/schema/schema.js +7 -5
- package/package.json +2 -2
- package/src/index.ts +1 -0
@@ -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.
|
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.
|
35
|
+
"@palmares/databases": "0.1.6",
|
36
36
|
"@palmares/server": "0.1.4"
|
37
37
|
},
|
38
38
|
"scripts": {
|
package/src/index.ts
CHANGED