minimonolith 0.12.2 → 0.13.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
@@ -126,8 +126,9 @@ This file validates the `get:id` method's input, ensuring that the provided `id`
126
126
  import { z, DB_VALIDATION } from 'minimonolith';
127
127
 
128
128
  export default (MODELS, ROUTE_CODE) => ({
129
- id: z.string().superRefine(
130
- DB_VALIDATION.exists(MODELS.todo, 'id', { parseInt: true })
131
- ),
129
+ id: z.string()
130
+ .superRefine(DB_VALIDATION.isSafeInt('id'))
131
+ .transform(id => parseInt(id))
132
+ .superRefine(DB_VALIDATION.exists(MODELS.todo, 'id')),
132
133
  })
133
134
  ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "minimonolith",
3
3
  "type": "module",
4
- "version": "0.12.2",
4
+ "version": "0.13.1",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -1,8 +1,7 @@
1
1
  import { z } from 'zod';
2
2
 
3
3
  const DB_VALIDATION = {
4
- exists: (model, field, options=undefined) => async (rawValue, ctx) => {
5
- const value = options?.parseInt ? parseInt(rawValue) : rawValue;
4
+ exists: (model, field) => async (value, ctx) => {
6
5
  const valueCount = await model.count({ where: { [field]: value } });
7
6
  if (valueCount == 0) {
8
7
  ctx.addIssue({
@@ -12,8 +11,7 @@ const DB_VALIDATION = {
12
11
  }
13
12
  },
14
13
 
15
- notExists: (model, field, options=undefined) => async (rawValue, ctx) => {
16
- const value = options?.parseInt ? parseInt(rawValue) : rawValue;
14
+ notExists: (model, field) => async (value, ctx) => {
17
15
  const valueCount = await model.count({ where: { [field]: value } });
18
16
  if (valueCount > 0) {
19
17
  ctx.addIssue({
@@ -22,6 +20,16 @@ const DB_VALIDATION = {
22
20
  });
23
21
  }
24
22
  },
23
+
24
+ isSafeInt: field => (rawValue, ctx) => {
25
+ const parsedValue = parseInt(rawValue, 10);
26
+ if (isNaN(parsedValue) || parsedValue.toString() !== rawValue) {
27
+ ctx.addIssue({
28
+ code: z.ZodIssueCode.custom,
29
+ message: field.toUpperCase()+'_NOT_A_SAFE_INT',
30
+ });
31
+ }
32
+ },
25
33
  };
26
34
 
27
35
  export { DB_VALIDATION };