sveltekit-superforms
Advanced tools
Comparing version 2.13.1 to 2.14.0
@@ -1,4 +0,4 @@ | ||
import type { Type } from 'arktype'; | ||
import { type Type } from 'arktype'; | ||
import { type ValidationAdapter, type RequiredDefaultsOptions, type Infer, type InferIn, type ClientValidationAdapter } from './adapters.js'; | ||
export declare const arktype: <T extends Type>(schema: T, options: RequiredDefaultsOptions<Infer<T>>) => ValidationAdapter<Infer<T>, InferIn<T>>; | ||
export declare const arktypeClient: <T extends Type>(schema: T) => ClientValidationAdapter<Infer<T>, InferIn<T>>; | ||
export declare const arktype: <T extends Type<unknown, any>>(schema: T, options: RequiredDefaultsOptions<Infer<T>>) => ValidationAdapter<Infer<T>, InferIn<T>>; | ||
export declare const arktypeClient: <T extends Type<unknown, any>>(schema: T) => ClientValidationAdapter<Infer<T>, InferIn<T>>; |
@@ -0,16 +1,18 @@ | ||
import { type } from 'arktype'; | ||
import { createAdapter, createJsonSchema } from './adapters.js'; | ||
import { memoize } from '../memoize.js'; | ||
async function validate(schema, data) { | ||
async function _validate(schema, data) { | ||
const result = schema(data); | ||
if (result.problems == null) { | ||
if (!(result instanceof type.errors)) { | ||
return { | ||
data: result.data, | ||
data: result, | ||
success: true | ||
}; | ||
} | ||
const issues = []; | ||
for (const error of result) { | ||
issues.push({ message: error.message, path: error.path }); | ||
} | ||
return { | ||
issues: Array.from(result.problems).map(({ message, path }) => ({ | ||
message, | ||
path | ||
})), | ||
issues, | ||
success: false | ||
@@ -24,18 +26,3 @@ }; | ||
jsonSchema: createJsonSchema(options), | ||
async validate(data) { | ||
const result = schema(data); | ||
if (result.problems == null) { | ||
return { | ||
data: result.data, | ||
success: true | ||
}; | ||
} | ||
return { | ||
issues: Array.from(result.problems).map(({ message, path }) => ({ | ||
message, | ||
path | ||
})), | ||
success: false | ||
}; | ||
} | ||
validate: async (data) => _validate(schema, data) | ||
}); | ||
@@ -46,3 +33,3 @@ } | ||
superFormValidationLibrary: 'arktype', | ||
validate: async (data) => validate(schema, data) | ||
validate: async (data) => _validate(schema, data) | ||
}; | ||
@@ -49,0 +36,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import type { AnyZodObject, ZodDefault, ZodEffects, ZodType, ZodTypeDef, ZodUnion } from 'zod'; | ||
import type { AnyZodObject, ZodDefault, ZodEffects, ZodErrorMap, ZodType, ZodTypeDef, ZodUnion } from 'zod'; | ||
import type { JSONSchema7 } from 'json-schema'; | ||
@@ -15,5 +15,8 @@ import { type AdapterOptions, type ValidationAdapter, type Infer, type InferIn, type ClientValidationAdapter } from './adapters.js'; | ||
export declare const zod: <T extends ZodValidation<ZodObjectTypes>>(schema: T, options?: (AdapterOptions<Infer<T>> & { | ||
errorMap?: ZodErrorMap | undefined; | ||
config?: Partial<Options> | undefined; | ||
}) | undefined) => ValidationAdapter<Infer<T>, InferIn<T>>; | ||
export declare const zodClient: <T extends ZodValidation<ZodObjectTypes>>(schema: T) => ClientValidationAdapter<Infer<T>, InferIn<T>>; | ||
export declare const zodClient: <T extends ZodValidation<ZodObjectTypes>>(schema: T, options?: { | ||
errorMap?: ZodErrorMap | undefined; | ||
} | undefined) => ClientValidationAdapter<Infer<T>, InferIn<T>>; | ||
export {}; |
@@ -14,4 +14,4 @@ import { createAdapter } from './adapters.js'; | ||
}; | ||
async function validate(schema, data) { | ||
const result = await schema.safeParseAsync(data); | ||
async function validate(schema, data, errorMap) { | ||
const result = await schema.safeParseAsync(data, { errorMap }); | ||
if (result.success) { | ||
@@ -31,3 +31,3 @@ return { | ||
superFormValidationLibrary: 'zod', | ||
validate: async (data) => validate(schema, data), | ||
validate: async (data) => validate(schema, data, options?.errorMap), | ||
jsonSchema: options?.jsonSchema ?? zodToJSONSchema(schema, options?.config), | ||
@@ -37,6 +37,6 @@ defaults: options?.defaults | ||
} | ||
function _zodClient(schema) { | ||
function _zodClient(schema, options) { | ||
return { | ||
superFormValidationLibrary: 'zod', | ||
validate: async (data) => validate(schema, data) | ||
validate: async (data) => validate(schema, data, options?.errorMap) | ||
}; | ||
@@ -43,0 +43,0 @@ } |
@@ -202,2 +202,11 @@ import { SchemaError } from '../errors.js'; | ||
} | ||
// Check if a Record type is used for additionalProperties | ||
if (info.additionalProperties && info.types.includes('object')) { | ||
const additionalInfo = schemaInfo(info.additionalProperties, info.isOptional, path); | ||
if (additionalInfo.properties && additionalInfo.types.includes('object')) { | ||
for (const [key] of Object.entries(additionalInfo.properties)) { | ||
output[key] = _defaultTypes(additionalInfo.properties[key], !additionalInfo.required?.includes(key), [...path, key]); | ||
} | ||
} | ||
} | ||
if (info.isNullable && !output.__types.includes('null')) { | ||
@@ -204,0 +213,0 @@ output.__types.push('null'); |
@@ -13,2 +13,5 @@ import type { JSONSchema7, JSONSchema7Definition, JSONSchema7TypeName } from 'json-schema'; | ||
}; | ||
additionalProperties?: { | ||
[key: string]: JSONSchema7; | ||
}; | ||
required?: string[]; | ||
@@ -15,0 +18,0 @@ }; |
@@ -20,2 +20,7 @@ import { assertSchema } from '../utils.js'; | ||
: undefined; | ||
const additionalProperties = schema.additionalProperties && | ||
typeof schema.additionalProperties === 'object' && | ||
types.includes('object') | ||
? Object.fromEntries(Object.entries(schema.additionalProperties).filter(([, value]) => typeof value !== 'boolean')) | ||
: undefined; | ||
const properties = schema.properties && types.includes('object') | ||
@@ -33,2 +38,3 @@ ? Object.fromEntries(Object.entries(schema.properties).filter(([, value]) => typeof value !== 'boolean')) | ||
properties, | ||
additionalProperties, | ||
required: schema.required | ||
@@ -35,0 +41,0 @@ }; |
{ | ||
"name": "sveltekit-superforms", | ||
"version": "2.13.1", | ||
"version": "2.14.0", | ||
"author": "Andreas Söderlund <ciscoheat@gmail.com> (https://blog.encodeart.dev)", | ||
@@ -91,12 +91,12 @@ "description": "Making SvelteKit forms a pleasure to use!", | ||
"@exodus/schemasafe": "^1.3.0", | ||
"@sinclair/typebox": ">=0.32.27 <1", | ||
"@sinclair/typebox": ">=0.32.30 <1", | ||
"@sveltejs/kit": "1.x || 2.x", | ||
"@vinejs/vine": "^1.7.1", | ||
"arktype": "1.0.29-alpha", | ||
"@vinejs/vine": "^1.8.0", | ||
"arktype": ">=2.0.0-dev.15", | ||
"joi": "^17.13.1", | ||
"superstruct": "^1.0.3", | ||
"superstruct": "^1.0.4", | ||
"svelte": "3.x || 4.x || >=5.0.0-next.51", | ||
"valibot": ">=0.28.1 <1", | ||
"yup": "^1.3.3", | ||
"zod": "^3.23.5" | ||
"valibot": ">=0.28.1 <=0.30.0", | ||
"yup": "^1.4.0", | ||
"zod": "^3.23.8" | ||
}, | ||
@@ -135,12 +135,12 @@ "peerDependenciesMeta": { | ||
"@gcornut/valibot-json-schema": "^0.0.27", | ||
"@sinclair/typebox": "^0.32.27", | ||
"@sinclair/typebox": "^0.32.30", | ||
"@sodaru/yup-to-json-schema": "^2.0.1", | ||
"@vinejs/vine": "^1.8.0", | ||
"arktype": "1.0.29-alpha", | ||
"arktype": "2.0.0-dev.15", | ||
"joi": "^17.13.1", | ||
"json-schema-to-ts": "^3.0.1", | ||
"json-schema-to-ts": "^3.1.0", | ||
"superstruct": "^1.0.4", | ||
"valibot": "^0.30.0", | ||
"yup": "^1.4.0", | ||
"zod": "^3.23.5", | ||
"zod": "^3.23.8", | ||
"zod-to-json-schema": "^3.23.0" | ||
@@ -155,8 +155,8 @@ }, | ||
"devDependencies": { | ||
"@sveltejs/adapter-auto": "^3.2.0", | ||
"@sveltejs/kit": "^2.5.7", | ||
"@sveltejs/adapter-auto": "^3.2.1", | ||
"@sveltejs/kit": "^2.5.10", | ||
"@sveltejs/package": "^2.3.1", | ||
"@sveltejs/vite-plugin-svelte": "^3.1.0", | ||
"@types/json-schema": "^7.0.15", | ||
"@types/node": "^20.12.8", | ||
"@types/node": "^20.12.12", | ||
"@types/throttle-debounce": "^5.0.2", | ||
@@ -173,6 +173,6 @@ "@types/uuid": "^9.0.8", | ||
"prettier-plugin-svelte": "^3.2.3", | ||
"publint": "^0.2.7", | ||
"sass": "^1.76.0", | ||
"svelte": "5.0.0-next.121", | ||
"svelte-check": "^3.7.0", | ||
"publint": "^0.2.8", | ||
"sass": "^1.77.2", | ||
"svelte": "5.0.0-next.137", | ||
"svelte-check": "^3.7.1", | ||
"svelte-french-toast": "^1.2.0", | ||
@@ -186,3 +186,3 @@ "sveltekit-flash-message": "^2.4.4", | ||
"vite": "^5.2.11", | ||
"vitest": "^1.5.3" | ||
"vitest": "^1.6.0" | ||
}, | ||
@@ -189,0 +189,0 @@ "svelte": "./dist/index.js", |
Sorry, the diff of this file is too big to display
278971
5908