@stacksjs/validation 0.75.64 → 0.75.65
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/dist/runtime.d.ts +38 -0
- package/dist/runtime.js +2 -0
- package/package.json +13 -7
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ConditionalAPI,
|
|
3
|
+
ConditionalRecord,
|
|
4
|
+
InferObjectShape,
|
|
5
|
+
InferredArrayValidator,
|
|
6
|
+
InferredEnumValidator,
|
|
7
|
+
InferValidatorValue,
|
|
8
|
+
ObjectWithContextValidator,
|
|
9
|
+
ValidatorShape,
|
|
10
|
+
ValidatorWithConditionals,
|
|
11
|
+
} from './schema';
|
|
12
|
+
/**
|
|
13
|
+
* Schema-declaration entrypoint.
|
|
14
|
+
*
|
|
15
|
+
* The package root also carries `./validator`, which is where the weight is:
|
|
16
|
+
* it reaches `@stacksjs/path`, `@stacksjs/strings`, `@stacksjs/utils` and
|
|
17
|
+
* `@stacksjs/error-handling` to validate an incoming request. Measured in
|
|
18
|
+
* fresh processes, `./schema` costs ~16 ms and `./validator` ~149 ms.
|
|
19
|
+
*
|
|
20
|
+
* Almost nothing needs the second one. Of the framework's 263 files importing
|
|
21
|
+
* this package, 254 import exactly `{ schema }` - every model declaring
|
|
22
|
+
* `validation: { rule: schema.string() }` - and the server's global injection
|
|
23
|
+
* took `schema` alone too. Two files use `validate`, and they keep the root.
|
|
24
|
+
*
|
|
25
|
+
* Export order is load-bearing, for the same reason it is in the barrel: bind
|
|
26
|
+
* `schema` before the `@stacksjs/ts-validation` star re-export, so a consumer
|
|
27
|
+
* arriving mid-evaluation through the auto-imports graph sees the Proxy rather
|
|
28
|
+
* than ts-validation's own `schema` symbol.
|
|
29
|
+
*/
|
|
30
|
+
export { schema } from './schema';
|
|
31
|
+
// Conditional validation surface (stacksjs/stacks#1890).
|
|
32
|
+
export {
|
|
33
|
+
applyConditionals,
|
|
34
|
+
objectWithContext,
|
|
35
|
+
shouldApplyConditional,
|
|
36
|
+
withConditionals,
|
|
37
|
+
} from './schema';
|
|
38
|
+
export * from '@stacksjs/ts-validation';
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import{v as h}from"@stacksjs/ts-validation";var j=new Set(["image/jpeg","image/png","image/webp","image/gif","image/avif"]);function g(e){return e.mimetype??e.mimeType}function k(e){return e.originalName??e.name}function A(e){let t=k(e);if(!t)return null;let n=t.lastIndexOf(".");if(n<=0||n===t.length-1)return null;let a=t.slice(n+1).toLowerCase();return/^[a-z0-9]+$/.test(a)?a:null}class b{_required=!1;_rules=[];required(){return this._required=!0,this}image(){return this._rules.push((e)=>{let t=g(e);if(!t||!j.has(t.toLowerCase()))return`must be an image (got ${t||"no mimetype"})`;return null}),this}mimeTypes(e){let t=new Set(e.map((n)=>n.toLowerCase()));return this._rules.push((n)=>{let a=g(n);if(!a||!t.has(a.toLowerCase()))return`must be one of: ${e.join(", ")} (got ${a||"no mimetype"})`;return null}),this}maxBytes(e){return this._rules.push((t)=>{if(typeof t.size!=="number")return"has no size - cannot enforce maxBytes";if(t.size>e)return`is too large: ${t.size} bytes exceeds the ${e}-byte cap`;return null}),this}minBytes(e){return this._rules.push((t)=>{if(typeof t.size!=="number")return"has no size - cannot enforce minBytes";if(t.size<e)return`is too small: ${t.size} bytes is below the ${e}-byte minimum`;return null}),this}extensions(e){let t=new Set(e.map((n)=>n.toLowerCase().replace(/^\./,"")));return this._rules.push((n)=>{let a=A(n);if(!a||!t.has(a))return`must have one of these extensions: ${e.join(", ")} (got ${a?`.${a}`:"no extension"})`;return null}),this}custom(e){return this._rules.push(e),this}validate(e){if(e===null||e===void 0){if(this._required)return{valid:!1,errors:[{message:"is required"}]};return{valid:!0}}if(typeof e!=="object"||typeof e.size!=="number")return{valid:!1,errors:[{message:"must be an uploaded file"}]};let t=e,n=[];for(let a of this._rules){let i=a(t);if(i)n.push({message:i})}return n.length===0?{valid:!0}:{valid:!1,errors:n}}}function x(){return new b}function O(e,t,n){if(!this.__conditionals)this.__conditionals=[];return this.__conditionals.push({field:e,match:t,refine:n}),this}function R(){return this.optional(),this}function o(e){let t=e;return t.when=O,t.sometimes=R,t}function m(e,t){if(t===null||t===void 0)return!1;let n=t[e.field];if(typeof e.match==="function")return e.match(n);return n===e.match}function u(e,t){let n=e.__conditionals;if(!n||n.length===0)return e;let a=null;for(let i of n){if(!m(i,t))continue;if(a===null)a=W(e);a=i.refine(a)}return a??e}function W(e){let t=Object.create(Object.getPrototypeOf(e));Object.assign(t,e);let n=e.rules;if(Array.isArray(n))t.rules=n.slice();return t}function c(e){let t=e??{},n=!0,a={name:"object",get isRequired(){return n},getRules:()=>[],test(r){return this.validate(r).valid},isPartOfShape:!1,shape(r){return t=r,a},getShape(){return t},required(){return n=!0,i},optional(){return n=!1,i},validate(r){let l=r===void 0||r===null;if(!n&&l)return{valid:!0,errors:this.isPartOfShape?{}:[]};if(n&&l)return this.isPartOfShape?{valid:!1,errors:{object:[{message:"value is required"}]}}:{valid:!1,errors:[{message:"value is required"}]};if(typeof r!=="object"||Array.isArray(r))return this.isPartOfShape?{valid:!1,errors:{object:[{message:"Must be an object"}]}}:{valid:!1,errors:[{message:"Must be an object"}]};let V=r,f={},p=!1;for(let s of Object.keys(t)){let y=t[s];if(!y)continue;let C=u(y,V),I=V[s],d=C.validate(I);if(d.valid)continue;if(p=!0,!Array.isArray(d.errors))for(let[S,w]of Object.entries(d.errors))f[`${s}.${S}`]=w;else f[s]=d.errors}if(p)return{valid:!1,errors:f};return{valid:!0,errors:{}}}},i=o(a);return i}var v=new Set(["array","bigint","binary","blob","boolean","custom","date","datetime","decimal","double","enum","float","integer","json","number","password","smallint","string","text","time","timestamp","timestampTz","unix"]);function N(e){if(e instanceof Date)return!Number.isNaN(e.getTime());if(typeof e!=="string"||!/^\d{4}-\d{2}-\d{2}$/.test(e))return!1;let t=new Date(`${e}T00:00:00.000Z`);return!Number.isNaN(t.getTime())&&t.toISOString().slice(0,10)===e}function _(){let e=h.custom(N,"Must be a valid date");return e.name="date",o(e)}var P=/^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?$/;function F(e){if(e instanceof Date)return!Number.isNaN(e.getTime());if(typeof e==="number")return Number.isFinite(e)&&e>=0;if(typeof e!=="string")return!1;let t=e.trim();if(!t)return!1;if(/^\d+$/.test(t))return!0;if(P.test(t))return!Number.isNaN(new Date(t.replace(" ","T")).getTime());return!Number.isNaN(new Date(t).getTime())}function E(){let e=h.custom(F,"Must be a valid timestamp");return e.name="timestamp",o(e)}var T=Object.create(null),L=new Proxy(h,{get(e,t,n){if(t==="file")return x;if(t==="object")return c;if(t==="date")return _;if(t==="timestamp")return E;if(typeof t==="string"){let a=T[t];if(a)return a;if(!v.has(t))return Reflect.get(e,t,n);let i=(...r)=>{let l=Reflect.get(e,t,n);return o(l(...r))};return T[t]=i,i}return Reflect.get(e,t,n)}});export*from"@stacksjs/ts-validation";export{u as applyConditionals,c as objectWithContext,L as schema,m as shouldApplyConditional,o as withConditionals};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/validation",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.65",
|
|
6
6
|
"description": "The Stacks Validation ways.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -32,6 +32,12 @@
|
|
|
32
32
|
"import": "./dist/index.js",
|
|
33
33
|
"default": "./dist/index.js"
|
|
34
34
|
},
|
|
35
|
+
"./runtime": {
|
|
36
|
+
"types": "./dist/runtime.d.ts",
|
|
37
|
+
"bun": "./dist/runtime.js",
|
|
38
|
+
"import": "./dist/runtime.js",
|
|
39
|
+
"default": "./dist/runtime.js"
|
|
40
|
+
},
|
|
35
41
|
"./*": {
|
|
36
42
|
"types": "./dist/*.d.ts",
|
|
37
43
|
"bun": "./dist/*.js",
|
|
@@ -58,18 +64,18 @@
|
|
|
58
64
|
"prepublishOnly": "bun run build"
|
|
59
65
|
},
|
|
60
66
|
"dependencies": {
|
|
61
|
-
"@stacksjs/error-handling": "0.75.
|
|
62
|
-
"@stacksjs/path": "0.75.
|
|
63
|
-
"@stacksjs/strings": "0.75.
|
|
67
|
+
"@stacksjs/error-handling": "0.75.65",
|
|
68
|
+
"@stacksjs/path": "0.75.65",
|
|
69
|
+
"@stacksjs/strings": "0.75.65",
|
|
64
70
|
"@stacksjs/ts-validation": "^0.5.6",
|
|
65
|
-
"@stacksjs/types": "0.75.
|
|
66
|
-
"@stacksjs/utils": "0.75.
|
|
71
|
+
"@stacksjs/types": "0.75.65",
|
|
72
|
+
"@stacksjs/utils": "0.75.65"
|
|
67
73
|
},
|
|
68
74
|
"devDependencies": {
|
|
69
75
|
"better-dx": "^0.2.24"
|
|
70
76
|
},
|
|
71
77
|
"peerDependencies": {
|
|
72
|
-
"@stacksjs/database": "0.75.
|
|
78
|
+
"@stacksjs/database": "0.75.65"
|
|
73
79
|
},
|
|
74
80
|
"peerDependenciesMeta": {
|
|
75
81
|
"@stacksjs/database": {
|