@lunora/values 1.0.0-alpha.33 → 1.0.0-alpha.34

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/index.d.mts CHANGED
@@ -116,8 +116,8 @@ interface Validator<T = unknown> extends StandardSchemaV1<T, T> {
116
116
  * `schema` fragments (later wins on conflicting keys).
117
117
  */
118
118
  meta: (options: MetaOptions) => Validator<T>;
119
- parse: (value: unknown) => T;
120
- safeParse: (value: unknown) => {
119
+ parse: (value: unknown, options?: ParseOptions) => T;
120
+ safeParse: (value: unknown, options?: ParseOptions) => {
121
121
  error: ValidationError;
122
122
  ok: false;
123
123
  } | {
@@ -270,6 +270,20 @@ interface NumberColumnValidator extends ColumnValidator<number, number> {
270
270
  /** Require a value strictly greater than zero (`exclusiveMinimum: 0`). */
271
271
  positive: () => NumberColumnValidator;
272
272
  }
273
+ /**
274
+ * A {@link ColumnValidator} for `v.object(...)`, carrying the `.strip()` opt-out.
275
+ */
276
+ interface ObjectColumnValidator<T> extends ColumnValidator<T, T> {
277
+ /**
278
+ * Drop keys this shape does not declare, even under `.output()`.
279
+ *
280
+ * Only meaningful there — input parsing strips already. Say it when the
281
+ * narrowing is deliberate (trimming an internal field off a row before it
282
+ * reaches a client); without it, an undeclared key on the way OUT is an
283
+ * error, because the alternative is deleting server data silently.
284
+ */
285
+ strip: () => ObjectColumnValidator<T>;
286
+ }
273
287
  /**
274
288
  * A {@link ColumnValidator} for `v.array(...)` with ergonomic length-refinement
275
289
  * shortcuts — see {@link StringColumnValidator} for the delegation pattern.
@@ -292,6 +306,11 @@ type SelectShape<S extends Record<string, Validator>> = { [K in keyof S]: InferS
292
306
  * become optional keys.
293
307
  */
294
308
  type InsertShape<S extends Record<string, Validator>> = { [K in keyof S as undefined extends InferInsert<S[K]> ? K : never]?: Exclude<InferInsert<S[K]>, undefined>; } & { [K in keyof S as undefined extends InferInsert<S[K]> ? never : K]: InferInsert<S[K]>; };
309
+ /** Options for {@link Validator.parse} / {@link Validator.safeParse}. */
310
+ interface ParseOptions {
311
+ /** See {@link ParseContext.rejectUnknownKeys}. */
312
+ rejectUnknownKeys?: boolean;
313
+ }
295
314
  declare const string: () => StringColumnValidator;
296
315
  declare const number: () => NumberColumnValidator;
297
316
  /** Epoch-millisecond timestamp (`number`). Pair with `.defaultNow()` for an insert-time clock. */
@@ -346,7 +365,7 @@ declare const array: <V extends Validator>(inner: V) => ArrayColumnValidator<Inf
346
365
  type OptionalizeShape<M> = { [K in keyof M as undefined extends M[K] ? K : never]?: M[K]; } & { [K in keyof M as undefined extends M[K] ? never : K]: M[K]; };
347
366
  type ObjectShape = Record<string, Validator>;
348
367
  type ObjectShapeType<S extends ObjectShape> = OptionalizeShape<{ [K in keyof S]: Infer<S[K]>; }>;
349
- declare const objectValidator: <S extends ObjectShape>(shape: S) => ColumnValidator<ObjectShapeType<S>, ObjectShapeType<S>>;
368
+ declare const objectValidator: <S extends ObjectShape>(shape: S, stripUnknown?: boolean) => ObjectColumnValidator<ObjectShapeType<S>>;
350
369
  declare const record: <K extends Validator<string>, V extends Validator>(keyValidator: K, valueValidator: V) => ColumnValidator<Record<Infer<K>, Infer<V>>, Record<Infer<K>, Infer<V>>>;
351
370
  declare const union: <Vs extends ReadonlyArray<Validator>>(...members: Vs) => ColumnValidator<Infer<Vs[number]>, Infer<Vs[number]>>;
352
371
  declare const optional: <V extends Validator>(inner: V) => ColumnValidator<Infer<V> | undefined, Infer<V> | undefined>;
@@ -643,4 +662,4 @@ declare const installCompiledValidatorMap: (validators: object, compiled: Compil
643
662
  * which case the interpreted loop below runs and owns the result (and any error).
644
663
  */
645
664
  declare const parseValidatorMap: (validators: ValidatorMap, source: Record<string, unknown>, label: string) => Record<string, unknown>;
646
- export { type ArrayColumnValidator, type CheckOptions, type Column, type ColumnMeta, type ColumnValidator, type CompiledValidatorMap, DEFER_VALIDATION, type GeoPoint, type Id, type Infer, type InferInsert, type InferSelect, type InferStandardInput, type InferStandardOutput, type InferValidatorMap, type InsertShape, type JsonSchema, type JsonSchemaFragment, type MetaOptions, type NumberColumnValidator, type SchemaNodeReader, type SelectShape, type ServerDefaultContext, type StringColumnValidator, type TimestampColumnValidator, ValidationError, type ValidationPath, type Validator, type ValidatorKind, type ValidatorMap, argsToJsonSchema, describeValue, formatPath, installCompiledValidatorMap, isOrWrapsFromValidator, jsonSchemaFromNode, objectSchemaFromNodes, optionalInner, parseValidatorMap, toJsonSchema, v };
665
+ export { type ArrayColumnValidator, type CheckOptions, type Column, type ColumnMeta, type ColumnValidator, type CompiledValidatorMap, DEFER_VALIDATION, type GeoPoint, type Id, type Infer, type InferInsert, type InferSelect, type InferStandardInput, type InferStandardOutput, type InferValidatorMap, type InsertShape, type JsonSchema, type JsonSchemaFragment, type MetaOptions, type NumberColumnValidator, type ObjectColumnValidator, type ParseOptions, type SchemaNodeReader, type SelectShape, type ServerDefaultContext, type StringColumnValidator, type TimestampColumnValidator, ValidationError, type ValidationPath, type Validator, type ValidatorKind, type ValidatorMap, argsToJsonSchema, describeValue, formatPath, installCompiledValidatorMap, isOrWrapsFromValidator, jsonSchemaFromNode, objectSchemaFromNodes, optionalInner, parseValidatorMap, toJsonSchema, v };
package/dist/index.d.ts CHANGED
@@ -116,8 +116,8 @@ interface Validator<T = unknown> extends StandardSchemaV1<T, T> {
116
116
  * `schema` fragments (later wins on conflicting keys).
117
117
  */
118
118
  meta: (options: MetaOptions) => Validator<T>;
119
- parse: (value: unknown) => T;
120
- safeParse: (value: unknown) => {
119
+ parse: (value: unknown, options?: ParseOptions) => T;
120
+ safeParse: (value: unknown, options?: ParseOptions) => {
121
121
  error: ValidationError;
122
122
  ok: false;
123
123
  } | {
@@ -270,6 +270,20 @@ interface NumberColumnValidator extends ColumnValidator<number, number> {
270
270
  /** Require a value strictly greater than zero (`exclusiveMinimum: 0`). */
271
271
  positive: () => NumberColumnValidator;
272
272
  }
273
+ /**
274
+ * A {@link ColumnValidator} for `v.object(...)`, carrying the `.strip()` opt-out.
275
+ */
276
+ interface ObjectColumnValidator<T> extends ColumnValidator<T, T> {
277
+ /**
278
+ * Drop keys this shape does not declare, even under `.output()`.
279
+ *
280
+ * Only meaningful there — input parsing strips already. Say it when the
281
+ * narrowing is deliberate (trimming an internal field off a row before it
282
+ * reaches a client); without it, an undeclared key on the way OUT is an
283
+ * error, because the alternative is deleting server data silently.
284
+ */
285
+ strip: () => ObjectColumnValidator<T>;
286
+ }
273
287
  /**
274
288
  * A {@link ColumnValidator} for `v.array(...)` with ergonomic length-refinement
275
289
  * shortcuts — see {@link StringColumnValidator} for the delegation pattern.
@@ -292,6 +306,11 @@ type SelectShape<S extends Record<string, Validator>> = { [K in keyof S]: InferS
292
306
  * become optional keys.
293
307
  */
294
308
  type InsertShape<S extends Record<string, Validator>> = { [K in keyof S as undefined extends InferInsert<S[K]> ? K : never]?: Exclude<InferInsert<S[K]>, undefined>; } & { [K in keyof S as undefined extends InferInsert<S[K]> ? never : K]: InferInsert<S[K]>; };
309
+ /** Options for {@link Validator.parse} / {@link Validator.safeParse}. */
310
+ interface ParseOptions {
311
+ /** See {@link ParseContext.rejectUnknownKeys}. */
312
+ rejectUnknownKeys?: boolean;
313
+ }
295
314
  declare const string: () => StringColumnValidator;
296
315
  declare const number: () => NumberColumnValidator;
297
316
  /** Epoch-millisecond timestamp (`number`). Pair with `.defaultNow()` for an insert-time clock. */
@@ -346,7 +365,7 @@ declare const array: <V extends Validator>(inner: V) => ArrayColumnValidator<Inf
346
365
  type OptionalizeShape<M> = { [K in keyof M as undefined extends M[K] ? K : never]?: M[K]; } & { [K in keyof M as undefined extends M[K] ? never : K]: M[K]; };
347
366
  type ObjectShape = Record<string, Validator>;
348
367
  type ObjectShapeType<S extends ObjectShape> = OptionalizeShape<{ [K in keyof S]: Infer<S[K]>; }>;
349
- declare const objectValidator: <S extends ObjectShape>(shape: S) => ColumnValidator<ObjectShapeType<S>, ObjectShapeType<S>>;
368
+ declare const objectValidator: <S extends ObjectShape>(shape: S, stripUnknown?: boolean) => ObjectColumnValidator<ObjectShapeType<S>>;
350
369
  declare const record: <K extends Validator<string>, V extends Validator>(keyValidator: K, valueValidator: V) => ColumnValidator<Record<Infer<K>, Infer<V>>, Record<Infer<K>, Infer<V>>>;
351
370
  declare const union: <Vs extends ReadonlyArray<Validator>>(...members: Vs) => ColumnValidator<Infer<Vs[number]>, Infer<Vs[number]>>;
352
371
  declare const optional: <V extends Validator>(inner: V) => ColumnValidator<Infer<V> | undefined, Infer<V> | undefined>;
@@ -643,4 +662,4 @@ declare const installCompiledValidatorMap: (validators: object, compiled: Compil
643
662
  * which case the interpreted loop below runs and owns the result (and any error).
644
663
  */
645
664
  declare const parseValidatorMap: (validators: ValidatorMap, source: Record<string, unknown>, label: string) => Record<string, unknown>;
646
- export { type ArrayColumnValidator, type CheckOptions, type Column, type ColumnMeta, type ColumnValidator, type CompiledValidatorMap, DEFER_VALIDATION, type GeoPoint, type Id, type Infer, type InferInsert, type InferSelect, type InferStandardInput, type InferStandardOutput, type InferValidatorMap, type InsertShape, type JsonSchema, type JsonSchemaFragment, type MetaOptions, type NumberColumnValidator, type SchemaNodeReader, type SelectShape, type ServerDefaultContext, type StringColumnValidator, type TimestampColumnValidator, ValidationError, type ValidationPath, type Validator, type ValidatorKind, type ValidatorMap, argsToJsonSchema, describeValue, formatPath, installCompiledValidatorMap, isOrWrapsFromValidator, jsonSchemaFromNode, objectSchemaFromNodes, optionalInner, parseValidatorMap, toJsonSchema, v };
665
+ export { type ArrayColumnValidator, type CheckOptions, type Column, type ColumnMeta, type ColumnValidator, type CompiledValidatorMap, DEFER_VALIDATION, type GeoPoint, type Id, type Infer, type InferInsert, type InferSelect, type InferStandardInput, type InferStandardOutput, type InferValidatorMap, type InsertShape, type JsonSchema, type JsonSchemaFragment, type MetaOptions, type NumberColumnValidator, type ObjectColumnValidator, type ParseOptions, type SchemaNodeReader, type SelectShape, type ServerDefaultContext, type StringColumnValidator, type TimestampColumnValidator, ValidationError, type ValidationPath, type Validator, type ValidatorKind, type ValidatorMap, argsToJsonSchema, describeValue, formatPath, installCompiledValidatorMap, isOrWrapsFromValidator, jsonSchemaFromNode, objectSchemaFromNodes, optionalInner, parseValidatorMap, toJsonSchema, v };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{ValidationError as a,describeValue as e,formatPath as t}from"./packem_shared/ValidationError-BLw-y3Pk.mjs";import{jsonSchemaFromNode as p,objectSchemaFromNodes as i}from"./packem_shared/jsonSchemaFromNode-DtU81nPr.mjs";import{argsToJsonSchema as l,toJsonSchema as d}from"./packem_shared/argsToJsonSchema-ujumcrdC.mjs";import{isOrWrapsFromValidator as c,optionalInner as f,v as V}from"./packem_shared/isOrWrapsFromValidator-BcMEVg8i.mjs";import{DEFER_VALIDATION as x,installCompiledValidatorMap as F,parseValidatorMap as S}from"./packem_shared/DEFER_VALIDATION-9tLHpbIF.mjs";export{x as DEFER_VALIDATION,a as ValidationError,l as argsToJsonSchema,e as describeValue,t as formatPath,F as installCompiledValidatorMap,c as isOrWrapsFromValidator,p as jsonSchemaFromNode,i as objectSchemaFromNodes,f as optionalInner,S as parseValidatorMap,d as toJsonSchema,V as v};
1
+ import{ValidationError as a,describeValue as e,formatPath as t}from"./packem_shared/ValidationError-BLw-y3Pk.mjs";import{jsonSchemaFromNode as p,objectSchemaFromNodes as i}from"./packem_shared/jsonSchemaFromNode-DtU81nPr.mjs";import{argsToJsonSchema as l,toJsonSchema as d}from"./packem_shared/argsToJsonSchema-ujumcrdC.mjs";import{isOrWrapsFromValidator as c,optionalInner as f,v as V}from"./packem_shared/isOrWrapsFromValidator-BdXulHj0.mjs";import{DEFER_VALIDATION as x,installCompiledValidatorMap as F,parseValidatorMap as S}from"./packem_shared/DEFER_VALIDATION-9tLHpbIF.mjs";export{x as DEFER_VALIDATION,a as ValidationError,l as argsToJsonSchema,e as describeValue,t as formatPath,F as installCompiledValidatorMap,c as isOrWrapsFromValidator,p as jsonSchemaFromNode,i as objectSchemaFromNodes,f as optionalInner,S as parseValidatorMap,d as toJsonSchema,V as v};
@@ -0,0 +1 @@
1
+ import{LunoraError as k}from"@lunora/errors";import{ValidationError as m,describeValue as w,formatPath as $}from"./ValidationError-BLw-y3Pk.mjs";const P=/^[^\s@]+@[^\s@.]+(?:\.[^\s@.]+)+$/u,I=e=>{if(!URL.canParse(e))return!1;const{protocol:r}=new URL(e);return r==="http:"||r==="https:"},_=new m("union branch probe miss (internal)",{expected:"",path:[],received:""});function u(e,r,s,i){if(e.probe===!0)throw _;const o=[...e.path],a=w(s,{literal:!i?.redactValue});throw new m(`Expected ${r} at ${$(o)}, received ${a}`,{expected:r,path:o,received:a})}const S=(e,r)=>{if(r===void 0)return e;const s=e?.constraints;return{...e,constraints:{...s,...r}}},l=(e,r,s)=>{const i=s?.column??{notNull:!0},o={__type:void 0,"~standard":{validate(n){const t=o.safeParse(n);return t.ok?{value:t.value}:{issues:[{message:t.error.message,path:t.error.path}]}},vendor:"lunora",version:1},_meta:{...s,column:i},_parse(n,t){return r(n,t)},kind:e,parse(n,t){return r(n,{path:[],...t})},safeParse(n,t){try{return{ok:!0,value:r(n,{path:[],...t})}}catch(c){if(c instanceof m)return{error:c,ok:!1};throw c}}},a=n=>l(e,r,{...s,column:{...i,...n}});switch(o.default=n=>a({defaultValue:n}),o.defaultNow=()=>a({defaultFn:()=>Date.now()}),o.unique=()=>a({unique:!0}),o.$defaultFn=n=>a({defaultFn:n}),o.$onUpdateFn=n=>a({onUpdateFn:n}),o.serverDefault=n=>a({serverDefault:n}),o.$type=(()=>a({})),o.nullable=()=>l(e,(t,c)=>t===null?null:r(t,c),{...s,column:{...i,notNull:!1}}),o.check=(n,t)=>{const c=typeof t=="string"?t:t?.message,f=typeof t=="string"?void 0:t?.schema;return l(e,(d,b)=>{const y=r(d,b);return n(y)||u(b,c??"value matching refinement",y,{redactValue:!0}),y},S(s,f))},o.meta=n=>{const t=n.description===void 0?n.schema:{description:n.description,...n.schema};return l(e,r,S(s,t))},e){case"array":{const n=o;n.min=t=>n.check(c=>c.length>=t,{message:`expected array length >= ${String(t)}`,schema:{minItems:t}}),n.max=t=>n.check(c=>c.length<=t,{message:`expected array length <= ${String(t)}`,schema:{maxItems:t}});break}case"number":{const n=o;n.min=t=>n.check(c=>c>=t,{message:`expected number >= ${String(t)}`,schema:{minimum:t}}),n.max=t=>n.check(c=>c<=t,{message:`expected number <= ${String(t)}`,schema:{maximum:t}}),n.int=()=>n.check(t=>Number.isInteger(t),{message:"expected an integer",schema:{type:"integer"}}),n.positive=()=>n.check(t=>t>0,{message:"expected a positive number",schema:{exclusiveMinimum:0}});break}case"string":{const n=o;n.min=t=>n.check(c=>c.length>=t,{message:`expected string length >= ${String(t)}`,schema:{minLength:t}}),n.max=t=>n.check(c=>c.length<=t,{message:`expected string length <= ${String(t)}`,schema:{maxLength:t}}),n.length=t=>n.check(c=>c.length===t,{message:`expected string length === ${String(t)}`,schema:{maxLength:t,minLength:t}}),n.pattern=t=>{const c=t.global||t.sticky?new RegExp(t.source,t.flags.replaceAll(/[gy]/gu,"")):t;return n.check(f=>c.test(f),{message:`expected string matching ${t.toString()}`,schema:{pattern:t.source}})},n.email=()=>n.check(t=>P.test(t),{message:"expected a valid email address",schema:{format:"email"}}),n.url=()=>n.check(t=>I(t),{message:"expected a valid URL",schema:{format:"uri"}});break}}return o},g=e=>e,p=e=>e,N=e=>e,O=e=>e,E=e=>e,L=()=>N(l("string",(e,r)=>(typeof e!="string"&&u(r,"string",e),e))),j=(e,r)=>((typeof e!="number"||!Number.isFinite(e))&&u(r,"number",e),e),R=()=>O(l("number",j)),V=()=>l("timestamp",j),F=()=>l("date",j),U=()=>p(l("boolean",(e,r)=>(typeof e!="boolean"&&u(r,"boolean",e),e))),C=()=>p(l("bigint",(e,r)=>(typeof e!="bigint"&&u(r,"bigint",e),e))),D=()=>p(l("null",(e,r)=>(e!==null&&u(r,"null",e),e))),T=()=>p(l("bytes",(e,r)=>(e instanceof ArrayBuffer||u(r,"ArrayBuffer",e),e))),q=e=>p(l("id",(r,s)=>(typeof r!="string"&&u(s,`Id<"${e}">`,r),r),{tableName:e})),B=e=>p(l("storage",(r,s)=>(typeof r!="string"&&u(s,"storage object key (string)",r),r),e===void 0?void 0:{bucket:e})),M=()=>p(l("geoPoint",(e,r)=>{(typeof e!="object"||e===null||Array.isArray(e))&&u(r,"geoPoint { lat, lng }",e);const s=e,{lat:i,lng:o}=s;return(typeof i!="number"||!Number.isFinite(i)||i<-90||i>90)&&(r.path.push("lat"),u(r,"latitude in [-90, 90]",i)),(typeof o!="number"||!Number.isFinite(o)||o<-180||o>180)&&(r.path.push("lng"),u(r,"longitude in [-180, 180]",o)),{lat:i,lng:o}})),x=e=>p(l("literal",(r,s)=>(r!==e&&u(s,`literal(${String(e)})`,r),r),{value:e})),K=e=>{const r=g(e);return E(l("array",(s,i)=>{Array.isArray(s)||u(i,"array",s);const{length:o}=s,a=[],{path:n}=i;for(let t=0;t<o;t+=1)n.push(t),a.push(r._parse(s[t],i)),n.pop();return a},{inner:e}))},v=(e,r)=>{const s=Object.keys(e).map(o=>{const a=g(e[o]);return{child:a,isOptional:a.kind==="optional",key:o}});if(s.some(({key:o})=>o==="__proto__"))throw new k("INTERNAL",'v.object: "__proto__" cannot be a declared field name — it collides with the Object.prototype accessor');const i=p(l("object",(o,a)=>{(typeof o!="object"||o===null||Array.isArray(o))&&u(a,"object",o);const n=o,t={},{path:c}=a;if(a.rejectUnknownKeys===!0&&r!==!0){const f=new Set(s.map(({key:d})=>d)),h=Object.keys(n).filter(d=>!f.has(d));if(h.length>0)throw new m(`object has ${String(h.length)} undeclared key(s): ${h.join(", ")}. Add them to the validator, or call .strip() on it to drop them on purpose.`,{expected:`only the declared keys (${s.map(({key:d})=>d).join(", ")})`,path:[...c],received:h.join(", ")})}for(const{child:f,isOptional:h,key:d}of s){const b=Object.hasOwn(n,d)?n[d]:void 0;b===void 0&&h||(c.push(d),t[d]=f._parse(b,a),c.pop())}return t},{shape:e}));return i.strip=()=>v(e,!0),i},W=(e,r)=>{const s=g(e),i=g(r);return p(l("record",(o,a)=>{(typeof o!="object"||o===null||Array.isArray(o))&&u(a,"record",o);const n=o,t=Object.create(null),{path:c}=a;for(const f of Object.keys(n)){c.push(f);const h=s._parse(f,a),d=i._parse(n[f],a);c.pop(),t[h]=d}return t},{keyValidator:e,valueValidator:r}))},z=(e,r,s,i)=>{const{path:o}=s;let a;for(const t of e)try{t._parse(r,s)}catch(c){if(!(c instanceof m))throw c;o.length=i,(a===void 0||c.path.length>a.path.length)&&(a=c)}if(e.length===1&&a!==void 0)throw a;const n=a===void 0?"":` (closest: expected ${a.expected} at ${$(a.path)})`;return u(s,`union of ${String(e.length)} member(s)${n}`,r)},G=(...e)=>{if(e.length===0)throw new k("INTERNAL","v.union requires at least one member");const r=e.map(s=>g(s));return p(l("union",(s,i)=>{const{path:o}=i,a=o.length,n=i.probe===!0;i.probe=!0;try{for(const t of r)try{return t._parse(s,i)}catch(c){if(!(c instanceof m))throw c;o.length=a}}finally{i.probe=n}if(n)throw _;return z(r,s,i,a)},{members:e}))},A=e=>{const r=g(e);return p(l("optional",(s,i)=>s===void 0?void 0:r._parse(s,i),{inner:e}))},H=e=>Object.fromEntries(Object.entries(e).map(([r,s])=>[r,g(s).kind==="optional"?s:A(s)])),J=()=>p(l("any",e=>e)),Q=e=>{const r=[];if(!e)return r;for(const s of e){const i=typeof s=="object"&&s!==null&&"key"in s?s.key:s;(typeof i=="string"||typeof i=="number")&&r.push(i)}return r},X=e=>{const r=e["~standard"];if(r?.version!==1||typeof r.validate!="function")throw new k("INTERNAL",'@lunora/values: v.from() expects a Standard Schema v1 object (missing or invalid "~standard")');const s=r.validate;return p(l("from",(i,o)=>{const a=s(i);if(a instanceof Promise||typeof a?.then=="function")throw new m("v.from(): async Standard Schema validators are not supported in args",{expected:"sync Standard Schema result",path:[...o.path],received:"Promise"});const n=a;if(n===null||typeof n!="object")throw new m("v.from(): Standard Schema validator returned a non-object result",{expected:"Standard Schema result object",path:[...o.path],received:w(n)});const t=a;if(t.issues!==void 0){const c=t.issues[0],f=c?.message??"Standard Schema validation failed";throw new m(f,{expected:"valid value",path:[...o.path,...Q(c?.path)],received:w(i)})}return t.value}))},Y=e=>{if(e.kind==="from")return!0;const r=e._meta;if(!r)return!1;const s=[r.inner,r.keyValidator,r.valueValidator];Array.isArray(r.members)&&s.push(...r.members),r.shape!==null&&typeof r.shape=="object"&&s.push(...Object.values(r.shape));for(const i of s)if(i!==null&&typeof i=="object"&&"kind"in i&&Y(i))return!0;return!1},te=e=>{if(e.kind==="optional")return e._meta?.inner},re={any:J,array:K,bigint:C,boolean:U,bytes:T,date:F,from:X,geoPoint:M,id:q,literal:x,null:D,number:R,object:v,optional:A,partial:H,record:W,storage:B,string:L,timestamp:V,union:G};export{Y as isOrWrapsFromValidator,te as optionalInner,re as v};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/values",
3
- "version": "1.0.0-alpha.33",
3
+ "version": "1.0.0-alpha.34",
4
4
  "description": "Validators for Lunora: the v.* validator suite with end-to-end return-type inference",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -1 +0,0 @@
1
- import{LunoraError as w}from"@lunora/errors";import{ValidationError as d,describeValue as y,formatPath as _}from"./ValidationError-BLw-y3Pk.mjs";const v=/^[^\s@]+@[^\s@.]+(?:\.[^\s@.]+)+$/u,P=e=>{if(!URL.canParse(e))return!1;const{protocol:r}=new URL(e);return r==="http:"||r==="https:"},$=new d("union branch probe miss (internal)",{expected:"",path:[],received:""});function u(e,r,s,o){if(e.probe===!0)throw $;const i=[...e.path],a=y(s,{literal:!o?.redactValue});throw new d(`Expected ${r} at ${_(i)}, received ${a}`,{expected:r,path:i,received:a})}const j=(e,r)=>{if(r===void 0)return e;const s=e?.constraints;return{...e,constraints:{...s,...r}}},l=(e,r,s)=>{const o=s?.column??{notNull:!0},i={__type:void 0,"~standard":{validate(n){const t=i.safeParse(n);return t.ok?{value:t.value}:{issues:[{message:t.error.message,path:t.error.path}]}},vendor:"lunora",version:1},_meta:{...s,column:o},_parse(n,t){return r(n,t)},kind:e,parse(n){return r(n,{path:[]})},safeParse(n){try{return{ok:!0,value:r(n,{path:[]})}}catch(t){if(t instanceof d)return{error:t,ok:!1};throw t}}},a=n=>l(e,r,{...s,column:{...o,...n}});switch(i.default=n=>a({defaultValue:n}),i.defaultNow=()=>a({defaultFn:()=>Date.now()}),i.unique=()=>a({unique:!0}),i.$defaultFn=n=>a({defaultFn:n}),i.$onUpdateFn=n=>a({onUpdateFn:n}),i.serverDefault=n=>a({serverDefault:n}),i.$type=(()=>a({})),i.nullable=()=>l(e,(t,c)=>t===null?null:r(t,c),{...s,column:{...o,notNull:!1}}),i.check=(n,t)=>{const c=typeof t=="string"?t:t?.message,f=typeof t=="string"?void 0:t?.schema;return l(e,(g,S)=>{const b=r(g,S);return n(b)||u(S,c??"value matching refinement",b,{redactValue:!0}),b},j(s,f))},i.meta=n=>{const t=n.description===void 0?n.schema:{description:n.description,...n.schema};return l(e,r,j(s,t))},e){case"array":{const n=i;n.min=t=>n.check(c=>c.length>=t,{message:`expected array length >= ${String(t)}`,schema:{minItems:t}}),n.max=t=>n.check(c=>c.length<=t,{message:`expected array length <= ${String(t)}`,schema:{maxItems:t}});break}case"number":{const n=i;n.min=t=>n.check(c=>c>=t,{message:`expected number >= ${String(t)}`,schema:{minimum:t}}),n.max=t=>n.check(c=>c<=t,{message:`expected number <= ${String(t)}`,schema:{maximum:t}}),n.int=()=>n.check(t=>Number.isInteger(t),{message:"expected an integer",schema:{type:"integer"}}),n.positive=()=>n.check(t=>t>0,{message:"expected a positive number",schema:{exclusiveMinimum:0}});break}case"string":{const n=i;n.min=t=>n.check(c=>c.length>=t,{message:`expected string length >= ${String(t)}`,schema:{minLength:t}}),n.max=t=>n.check(c=>c.length<=t,{message:`expected string length <= ${String(t)}`,schema:{maxLength:t}}),n.length=t=>n.check(c=>c.length===t,{message:`expected string length === ${String(t)}`,schema:{maxLength:t,minLength:t}}),n.pattern=t=>{const c=t.global||t.sticky?new RegExp(t.source,t.flags.replaceAll(/[gy]/gu,"")):t;return n.check(f=>c.test(f),{message:`expected string matching ${t.toString()}`,schema:{pattern:t.source}})},n.email=()=>n.check(t=>v.test(t),{message:"expected a valid email address",schema:{format:"email"}}),n.url=()=>n.check(t=>P(t),{message:"expected a valid URL",schema:{format:"uri"}});break}}return i},h=e=>e,p=e=>e,I=e=>e,N=e=>e,O=e=>e,E=()=>I(l("string",(e,r)=>(typeof e!="string"&&u(r,"string",e),e))),k=(e,r)=>((typeof e!="number"||!Number.isFinite(e))&&u(r,"number",e),e),L=()=>N(l("number",k)),R=()=>l("timestamp",k),V=()=>l("date",k),F=()=>p(l("boolean",(e,r)=>(typeof e!="boolean"&&u(r,"boolean",e),e))),U=()=>p(l("bigint",(e,r)=>(typeof e!="bigint"&&u(r,"bigint",e),e))),C=()=>p(l("null",(e,r)=>(e!==null&&u(r,"null",e),e))),D=()=>p(l("bytes",(e,r)=>(e instanceof ArrayBuffer||u(r,"ArrayBuffer",e),e))),T=e=>p(l("id",(r,s)=>(typeof r!="string"&&u(s,`Id<"${e}">`,r),r),{tableName:e})),q=e=>p(l("storage",(r,s)=>(typeof r!="string"&&u(s,"storage object key (string)",r),r),e===void 0?void 0:{bucket:e})),B=()=>p(l("geoPoint",(e,r)=>{(typeof e!="object"||e===null||Array.isArray(e))&&u(r,"geoPoint { lat, lng }",e);const s=e,{lat:o,lng:i}=s;return(typeof o!="number"||!Number.isFinite(o)||o<-90||o>90)&&(r.path.push("lat"),u(r,"latitude in [-90, 90]",o)),(typeof i!="number"||!Number.isFinite(i)||i<-180||i>180)&&(r.path.push("lng"),u(r,"longitude in [-180, 180]",i)),{lat:o,lng:i}})),M=e=>p(l("literal",(r,s)=>(r!==e&&u(s,`literal(${String(e)})`,r),r),{value:e})),x=e=>{const r=h(e);return O(l("array",(s,o)=>{Array.isArray(s)||u(o,"array",s);const{length:i}=s,a=[],{path:n}=o;for(let t=0;t<i;t+=1)n.push(t),a.push(r._parse(s[t],o)),n.pop();return a},{inner:e}))},K=e=>{const r=Object.keys(e).map(s=>{const o=h(e[s]);return{child:o,isOptional:o.kind==="optional",key:s}});if(r.some(({key:s})=>s==="__proto__"))throw new w("INTERNAL",'v.object: "__proto__" cannot be a declared field name — it collides with the Object.prototype accessor');return p(l("object",(s,o)=>{(typeof s!="object"||s===null||Array.isArray(s))&&u(o,"object",s);const i=s,a={},{path:n}=o;for(const{child:t,isOptional:c,key:f}of r){const m=Object.hasOwn(i,f)?i[f]:void 0;m===void 0&&c||(n.push(f),a[f]=t._parse(m,o),n.pop())}return a},{shape:e}))},W=(e,r)=>{const s=h(e),o=h(r);return p(l("record",(i,a)=>{(typeof i!="object"||i===null||Array.isArray(i))&&u(a,"record",i);const n=i,t=Object.create(null),{path:c}=a;for(const f of Object.keys(n)){c.push(f);const m=s._parse(f,a),g=o._parse(n[f],a);c.pop(),t[m]=g}return t},{keyValidator:e,valueValidator:r}))},z=(e,r,s,o)=>{const{path:i}=s;let a;for(const t of e)try{t._parse(r,s)}catch(c){if(!(c instanceof d))throw c;i.length=o,(a===void 0||c.path.length>a.path.length)&&(a=c)}if(e.length===1&&a!==void 0)throw a;const n=a===void 0?"":` (closest: expected ${a.expected} at ${_(a.path)})`;return u(s,`union of ${String(e.length)} member(s)${n}`,r)},G=(...e)=>{if(e.length===0)throw new w("INTERNAL","v.union requires at least one member");const r=e.map(s=>h(s));return p(l("union",(s,o)=>{const{path:i}=o,a=i.length,n=o.probe===!0;o.probe=!0;try{for(const t of r)try{return t._parse(s,o)}catch(c){if(!(c instanceof d))throw c;i.length=a}}finally{o.probe=n}if(n)throw $;return z(r,s,o,a)},{members:e}))},A=e=>{const r=h(e);return p(l("optional",(s,o)=>s===void 0?void 0:r._parse(s,o),{inner:e}))},H=e=>Object.fromEntries(Object.entries(e).map(([r,s])=>[r,h(s).kind==="optional"?s:A(s)])),J=()=>p(l("any",e=>e)),Q=e=>{const r=[];if(!e)return r;for(const s of e){const o=typeof s=="object"&&s!==null&&"key"in s?s.key:s;(typeof o=="string"||typeof o=="number")&&r.push(o)}return r},X=e=>{const r=e["~standard"];if(r?.version!==1||typeof r.validate!="function")throw new w("INTERNAL",'@lunora/values: v.from() expects a Standard Schema v1 object (missing or invalid "~standard")');const s=r.validate;return p(l("from",(o,i)=>{const a=s(o);if(a instanceof Promise||typeof a?.then=="function")throw new d("v.from(): async Standard Schema validators are not supported in args",{expected:"sync Standard Schema result",path:[...i.path],received:"Promise"});const n=a;if(n===null||typeof n!="object")throw new d("v.from(): Standard Schema validator returned a non-object result",{expected:"Standard Schema result object",path:[...i.path],received:y(n)});const t=a;if(t.issues!==void 0){const c=t.issues[0],f=c?.message??"Standard Schema validation failed";throw new d(f,{expected:"valid value",path:[...i.path,...Q(c?.path)],received:y(o)})}return t.value}))},Y=e=>{if(e.kind==="from")return!0;const r=e._meta;if(!r)return!1;const s=[r.inner,r.keyValidator,r.valueValidator];Array.isArray(r.members)&&s.push(...r.members),r.shape!==null&&typeof r.shape=="object"&&s.push(...Object.values(r.shape));for(const o of s)if(o!==null&&typeof o=="object"&&"kind"in o&&Y(o))return!0;return!1},te=e=>{if(e.kind==="optional")return e._meta?.inner},re={any:J,array:x,bigint:U,boolean:F,bytes:D,date:V,from:X,geoPoint:B,id:T,literal:M,null:C,number:L,object:K,optional:A,partial:H,record:W,storage:q,string:E,timestamp:R,union:G};export{Y as isOrWrapsFromValidator,te as optionalInner,re as v};