sveltekit-superforms
Advanced tools
Comparing version 0.5.17 to 0.5.18
@@ -185,2 +185,4 @@ import { enhance, applyAction } from '$app/forms'; | ||
continue; | ||
if (!f[key] && !previousForm[key]) | ||
continue; | ||
// Date comparison is a mess, since it can come from the server as undefined, | ||
@@ -193,2 +195,3 @@ // or be Invalid Date from a proxy. | ||
} | ||
//console.log('Field changed:', f, f[key], previousForm[key]); | ||
const validator = options.validators && options.validators[key]; | ||
@@ -195,0 +198,0 @@ if (validator) { |
@@ -167,5 +167,3 @@ import { z, ZodDefault, ZodNullable, ZodOptional, ZodString, ZodNumber, ZodBoolean, ZodDate, ZodArray, ZodEffects, ZodBigInt, ZodObject, ZodSymbol } from 'zod'; | ||
} | ||
if (!info.isNullable && | ||
!info.isOptional && | ||
(info.defaultValue === undefined || info.defaultValue === null)) { | ||
if (!info.isNullable && !info.isOptional) { | ||
output.required = true; | ||
@@ -177,14 +175,26 @@ } | ||
return constraint(key, typeInfo[key]); | ||
}); | ||
}, (constraint) => !!constraint); | ||
} | ||
function meta(schema) { | ||
return { | ||
types: _mapSchema(schema, (obj) => zodTypeInfo(obj).zodType.constructor.name) | ||
types: _mapSchema(schema, (obj) => { | ||
let type = zodTypeInfo(obj).zodType; | ||
let name = ''; | ||
let depth = 0; | ||
while (type instanceof ZodArray) { | ||
name += 'ZodArray<'; | ||
depth++; | ||
type = type._def.type; | ||
} | ||
return name + type.constructor.name + '>'.repeat(depth); | ||
}) | ||
}; | ||
} | ||
/////////////////////////////////////////////////////////////////////////// | ||
function _mapSchema(schema, factory) { | ||
function _mapSchema(schema, factory, filter) { | ||
const keys = schema.keyof().Values; | ||
return Object.fromEntries(Object.keys(keys).map((key) => [key, factory(schema.shape[key], key)])); | ||
return Object.fromEntries(Object.keys(keys) | ||
.map((key) => [key, factory(schema.shape[key], key)]) | ||
.filter((entry) => (filter ? filter(entry[1]) : true))); | ||
} | ||
/////////////////////////////////////////////////////////////////////////// |
import { fail, json } from '@sveltejs/kit'; | ||
import { parse, stringify } from 'devalue'; | ||
import { checkMissingFields, entityData, valueOrDefault, zodTypeInfo } from './entity'; | ||
import { z, ZodAny, ZodString, ZodNumber, ZodBoolean, ZodDate, ZodLiteral, ZodUnion, ZodArray, ZodBigInt, ZodEnum } from 'zod'; | ||
import { z, ZodAny, ZodString, ZodNumber, ZodBoolean, ZodDate, ZodLiteral, ZodUnion, ZodArray, ZodBigInt, ZodEnum, ZodNativeEnum, ZodSymbol } from 'zod'; | ||
export { defaultEntity } from './entity'; | ||
@@ -57,8 +57,8 @@ export function setError(form, field, error, options = { overwrite: false }) { | ||
} | ||
else if (zodType instanceof ZodBoolean) { | ||
return Boolean(value).valueOf(); | ||
} | ||
else if (zodType instanceof ZodDate) { | ||
return new Date(value ?? ''); | ||
} | ||
else if (zodType instanceof ZodBoolean) { | ||
return Boolean(value).valueOf(); | ||
} | ||
else if (zodType instanceof ZodArray) { | ||
@@ -68,2 +68,10 @@ const arrayType = zodTypeInfo(zodType._def.type); | ||
} | ||
else if (zodType instanceof ZodBigInt) { | ||
try { | ||
return BigInt(value ?? '.'); | ||
} | ||
catch { | ||
return NaN; | ||
} | ||
} | ||
else if (zodType instanceof ZodLiteral) { | ||
@@ -86,10 +94,16 @@ const literalType = typeof zodType.value; | ||
} | ||
else if (zodType instanceof ZodBigInt) { | ||
try { | ||
return BigInt(value ?? '.'); | ||
else if (zodType instanceof ZodNativeEnum) { | ||
//console.log(field, typeof value, value, zodType.enum); | ||
if (value in zodType.enum) { | ||
const enumValue = zodType.enum[value]; | ||
if (typeof enumValue === 'number') | ||
return enumValue; | ||
else if (enumValue in zodType.enum) | ||
return zodType.enum[enumValue]; | ||
} | ||
catch { | ||
return NaN; | ||
} | ||
return undefined; | ||
} | ||
else if (zodType instanceof ZodSymbol) { | ||
return Symbol(value); | ||
} | ||
throw new Error('Unsupported Zod default type: ' + zodType.constructor.name); | ||
@@ -96,0 +110,0 @@ } |
{ | ||
"name": "sveltekit-superforms", | ||
"version": "0.5.17", | ||
"version": "0.5.18", | ||
"author": "Andreas Söderlund <ciscoheat@gmail.com> (https://blog.encodeart.dev)", | ||
@@ -70,3 +70,3 @@ "description": "Supercharge your SvelteKit forms with this powerhouse of a library!", | ||
"eslint": "^8.35.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"eslint-config-prettier": "^8.7.0", | ||
"eslint-plugin-svelte3": "^4.0.0", | ||
@@ -81,2 +81,3 @@ "prettier": "^2.8.4", | ||
"sveltekit-flash-message": "^0.11.3", | ||
"sveltekit-rate-limiter": "^0.1.7", | ||
"tslib": "^2.5.0", | ||
@@ -86,3 +87,3 @@ "typescript": "^4.9.5", | ||
"vitest": "^0.25.8", | ||
"zod": "^3.21.0" | ||
"zod": "^3.21.2" | ||
}, | ||
@@ -89,0 +90,0 @@ "type": "module", |
84091
1376
23