@conform-to/zod
Advanced tools
Comparing version 0.3.1 to 0.4.0-pre.0
@@ -1,4 +0,5 @@ | ||
import { type Schema } from '@conform-to/dom'; | ||
import { type FieldsetConstraint } from '@conform-to/dom'; | ||
import * as z from 'zod'; | ||
export declare function resolve<Source extends z.ZodTypeAny>(source: Source): Schema<z.infer<Source>, Source>; | ||
export declare function getFieldsetConstraint<Source extends z.ZodTypeAny>(source: Source): FieldsetConstraint<z.infer<Source>>; | ||
export declare function getError(error: z.ZodError<any> | null, scope?: string[]): Array<[string, string]>; | ||
export declare function ifNonEmptyString(fn: (value: string) => unknown): (value: unknown) => unknown; |
231
index.js
@@ -29,177 +29,111 @@ 'use strict'; | ||
function cleanup(data) { | ||
if (typeof data === 'string' || typeof data === 'undefined' || data instanceof File) { | ||
return data !== '' ? data : undefined; | ||
} else if (Array.isArray(data)) { | ||
return data.map(item => cleanup(item)); | ||
} else if (data !== null && typeof data === 'object') { | ||
var result = {}; | ||
for (var [key, _value] of Object.entries(data)) { | ||
result[key] = cleanup(_value); | ||
function getFieldsetConstraint(source) { | ||
function getSchemaShape(schema) { | ||
if (schema instanceof z__namespace.ZodObject) { | ||
return schema.shape; | ||
} else if (schema instanceof z__namespace.ZodEffects) { | ||
return getSchemaShape(schema.innerType()); | ||
} else if (schema instanceof z__namespace.ZodOptional) { | ||
return getSchemaShape(schema.unwrap()); | ||
} | ||
return result; | ||
} else { | ||
throw new Error('Invalid data'); | ||
return null; | ||
} | ||
} | ||
function formatError(error) { | ||
var result = {}; | ||
function inferConstraint(schema) { | ||
var constraint = {}; | ||
var _loop = function _loop(issue) { | ||
dom.setValue(result, issue.path.flatMap(path => ['details', path]).concat('message'), prev => prev ? prev : issue.message); | ||
}; | ||
if (schema instanceof z__namespace.ZodEffects) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.innerType())); | ||
} else if (schema instanceof z__namespace.ZodOptional) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.unwrap())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z__namespace.ZodDefault) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.removeDefault())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z__namespace.ZodArray) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.element)), {}, { | ||
multiple: true | ||
}); | ||
} else if (schema instanceof z__namespace.ZodString) { | ||
for (var check of schema._def.checks) { | ||
switch (check.kind) { | ||
case 'min': | ||
if (!constraint.minLength || constraint.minLength < check.value) { | ||
constraint.minLength = check.value; | ||
} | ||
for (var issue of error.errors) { | ||
_loop(issue); | ||
} | ||
break; | ||
return result; | ||
} | ||
case 'max': | ||
if (!constraint.maxLength || constraint.maxLength > check.value) { | ||
constraint.maxLength = check.value; | ||
} | ||
function inferConstraint(schema) { | ||
var constraint = {}; | ||
break; | ||
if (schema instanceof z__namespace.ZodEffects) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.innerType())); | ||
} else if (schema instanceof z__namespace.ZodOptional) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.unwrap())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z__namespace.ZodDefault) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.removeDefault())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z__namespace.ZodArray) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.element)), {}, { | ||
multiple: true | ||
}); | ||
} else if (schema instanceof z__namespace.ZodString) { | ||
for (var check of schema._def.checks) { | ||
switch (check.kind) { | ||
case 'min': | ||
if (!constraint.minLength || constraint.minLength < check.value) { | ||
constraint.minLength = check.value; | ||
} | ||
case 'regex': | ||
if (!constraint.pattern) { | ||
constraint.pattern = check.regex.source; | ||
} | ||
break; | ||
break; | ||
} | ||
} | ||
} else if (schema instanceof z__namespace.ZodNumber) { | ||
for (var _check of schema._def.checks) { | ||
switch (_check.kind) { | ||
case 'min': | ||
if (!constraint.min || constraint.min < _check.value) { | ||
constraint.min = _check.value; | ||
} | ||
case 'max': | ||
if (!constraint.maxLength || constraint.maxLength > check.value) { | ||
constraint.maxLength = check.value; | ||
} | ||
break; | ||
break; | ||
case 'max': | ||
if (!constraint.max || constraint.max > _check.value) { | ||
constraint.max = _check.value; | ||
} | ||
case 'regex': | ||
if (!constraint.pattern) { | ||
constraint.pattern = check.regex.source; | ||
} | ||
break; | ||
break; | ||
} | ||
} | ||
} else if (schema instanceof z__namespace.ZodEnum) { | ||
constraint.pattern = schema.options.map(option => // To escape unsafe characters on regex | ||
option.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')).join('|'); | ||
} | ||
} else if (schema instanceof z__namespace.ZodNumber) { | ||
for (var _check of schema._def.checks) { | ||
switch (_check.kind) { | ||
case 'min': | ||
if (!constraint.min || constraint.min < _check.value) { | ||
constraint.min = _check.value; | ||
} | ||
break; | ||
case 'max': | ||
if (!constraint.max || constraint.max > _check.value) { | ||
constraint.max = _check.value; | ||
} | ||
break; | ||
} | ||
if (typeof constraint.required === 'undefined') { | ||
constraint.required = true; | ||
} | ||
} else if (schema instanceof z__namespace.ZodEnum) { | ||
constraint.pattern = schema.options.map(option => // To escape unsafe characters on regex | ||
option.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')).join('|'); | ||
} | ||
if (typeof constraint.required === 'undefined') { | ||
constraint.required = true; | ||
return constraint; | ||
} | ||
return constraint; | ||
} | ||
var shape = getSchemaShape(source); | ||
var result = {}; | ||
function getSchemaShape(schema) { | ||
if (schema instanceof z__namespace.ZodObject) { | ||
return schema.shape; | ||
} else if (schema instanceof z__namespace.ZodEffects) { | ||
return getSchemaShape(schema.innerType()); | ||
} else if (schema instanceof z__namespace.ZodOptional) { | ||
return getSchemaShape(schema.unwrap()); | ||
if (shape) { | ||
for (var [key, def] of Object.entries(shape)) { | ||
// @ts-expect-error | ||
result[key] = inferConstraint(def); | ||
} | ||
} | ||
return null; | ||
return result; | ||
} | ||
function getError(error, scope) { | ||
var _error$errors$reduce; | ||
function resolve(source) { | ||
return { | ||
source, | ||
constraint: new Proxy({}, { | ||
get(_target, key) { | ||
if (typeof key !== 'string') { | ||
return; | ||
} | ||
return (_error$errors$reduce = error === null || error === void 0 ? void 0 : error.errors.reduce((result, e) => { | ||
var name = dom.getName(e.path); | ||
var shape = getSchemaShape(source); | ||
var schema = shape === null || shape === void 0 ? void 0 : shape[key]; | ||
if (!schema) { | ||
return {}; | ||
} | ||
return inferConstraint(schema); | ||
} | ||
}), | ||
validate(form, submitter) { | ||
var payload = dom.getFormData(form, submitter); | ||
var submission = dom.createSubmission(payload); | ||
var value = cleanup(submission.form.value); | ||
var result = source.safeParse(value); | ||
var errors = !result.success ? result.error.errors.map(e => [dom.getName(e.path), e.message]) : []; | ||
dom.setFormError(form, errors); | ||
}, | ||
parse(payload) { | ||
var submission = dom.createSubmission(payload); | ||
if (submission.state !== 'accepted') { | ||
return submission; | ||
} | ||
var value = cleanup(submission.form.value); | ||
var result = source.safeParse(value); | ||
if (result.success) { | ||
return { | ||
state: 'accepted', | ||
data: result.data, | ||
form: submission.form | ||
}; | ||
} else { | ||
return { | ||
state: 'rejected', | ||
form: { | ||
// @ts-expect-error | ||
value: submission.form.value, | ||
error: formatError(result.error) | ||
} | ||
}; | ||
} | ||
if (!scope || scope.includes(name)) { | ||
result.push([name, e.message]); | ||
} | ||
}; | ||
return result; | ||
}, [])) !== null && _error$errors$reduce !== void 0 ? _error$errors$reduce : []; | ||
} | ||
@@ -220,3 +154,4 @@ function ifNonEmptyString(fn) { | ||
exports.getError = getError; | ||
exports.getFieldsetConstraint = getFieldsetConstraint; | ||
exports.ifNonEmptyString = ifNonEmptyString; | ||
exports.resolve = resolve; |
import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js'; | ||
import { getFormData, createSubmission, getName, setFormError, setValue } from '@conform-to/dom'; | ||
import { getName } from '@conform-to/dom'; | ||
import * as z from 'zod'; | ||
function cleanup(data) { | ||
if (typeof data === 'string' || typeof data === 'undefined' || data instanceof File) { | ||
return data !== '' ? data : undefined; | ||
} else if (Array.isArray(data)) { | ||
return data.map(item => cleanup(item)); | ||
} else if (data !== null && typeof data === 'object') { | ||
var result = {}; | ||
for (var [key, _value] of Object.entries(data)) { | ||
result[key] = cleanup(_value); | ||
function getFieldsetConstraint(source) { | ||
function getSchemaShape(schema) { | ||
if (schema instanceof z.ZodObject) { | ||
return schema.shape; | ||
} else if (schema instanceof z.ZodEffects) { | ||
return getSchemaShape(schema.innerType()); | ||
} else if (schema instanceof z.ZodOptional) { | ||
return getSchemaShape(schema.unwrap()); | ||
} | ||
return result; | ||
} else { | ||
throw new Error('Invalid data'); | ||
return null; | ||
} | ||
} | ||
function formatError(error) { | ||
var result = {}; | ||
function inferConstraint(schema) { | ||
var constraint = {}; | ||
var _loop = function _loop(issue) { | ||
setValue(result, issue.path.flatMap(path => ['details', path]).concat('message'), prev => prev ? prev : issue.message); | ||
}; | ||
if (schema instanceof z.ZodEffects) { | ||
constraint = _objectSpread2({}, inferConstraint(schema.innerType())); | ||
} else if (schema instanceof z.ZodOptional) { | ||
constraint = _objectSpread2(_objectSpread2({}, inferConstraint(schema.unwrap())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z.ZodDefault) { | ||
constraint = _objectSpread2(_objectSpread2({}, inferConstraint(schema.removeDefault())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z.ZodArray) { | ||
constraint = _objectSpread2(_objectSpread2({}, inferConstraint(schema.element)), {}, { | ||
multiple: true | ||
}); | ||
} else if (schema instanceof z.ZodString) { | ||
for (var check of schema._def.checks) { | ||
switch (check.kind) { | ||
case 'min': | ||
if (!constraint.minLength || constraint.minLength < check.value) { | ||
constraint.minLength = check.value; | ||
} | ||
for (var issue of error.errors) { | ||
_loop(issue); | ||
} | ||
break; | ||
return result; | ||
} | ||
case 'max': | ||
if (!constraint.maxLength || constraint.maxLength > check.value) { | ||
constraint.maxLength = check.value; | ||
} | ||
function inferConstraint(schema) { | ||
var constraint = {}; | ||
break; | ||
if (schema instanceof z.ZodEffects) { | ||
constraint = _objectSpread2({}, inferConstraint(schema.innerType())); | ||
} else if (schema instanceof z.ZodOptional) { | ||
constraint = _objectSpread2(_objectSpread2({}, inferConstraint(schema.unwrap())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z.ZodDefault) { | ||
constraint = _objectSpread2(_objectSpread2({}, inferConstraint(schema.removeDefault())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z.ZodArray) { | ||
constraint = _objectSpread2(_objectSpread2({}, inferConstraint(schema.element)), {}, { | ||
multiple: true | ||
}); | ||
} else if (schema instanceof z.ZodString) { | ||
for (var check of schema._def.checks) { | ||
switch (check.kind) { | ||
case 'min': | ||
if (!constraint.minLength || constraint.minLength < check.value) { | ||
constraint.minLength = check.value; | ||
} | ||
case 'regex': | ||
if (!constraint.pattern) { | ||
constraint.pattern = check.regex.source; | ||
} | ||
break; | ||
break; | ||
} | ||
} | ||
} else if (schema instanceof z.ZodNumber) { | ||
for (var _check of schema._def.checks) { | ||
switch (_check.kind) { | ||
case 'min': | ||
if (!constraint.min || constraint.min < _check.value) { | ||
constraint.min = _check.value; | ||
} | ||
case 'max': | ||
if (!constraint.maxLength || constraint.maxLength > check.value) { | ||
constraint.maxLength = check.value; | ||
} | ||
break; | ||
break; | ||
case 'max': | ||
if (!constraint.max || constraint.max > _check.value) { | ||
constraint.max = _check.value; | ||
} | ||
case 'regex': | ||
if (!constraint.pattern) { | ||
constraint.pattern = check.regex.source; | ||
} | ||
break; | ||
break; | ||
} | ||
} | ||
} else if (schema instanceof z.ZodEnum) { | ||
constraint.pattern = schema.options.map(option => // To escape unsafe characters on regex | ||
option.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')).join('|'); | ||
} | ||
} else if (schema instanceof z.ZodNumber) { | ||
for (var _check of schema._def.checks) { | ||
switch (_check.kind) { | ||
case 'min': | ||
if (!constraint.min || constraint.min < _check.value) { | ||
constraint.min = _check.value; | ||
} | ||
break; | ||
case 'max': | ||
if (!constraint.max || constraint.max > _check.value) { | ||
constraint.max = _check.value; | ||
} | ||
break; | ||
} | ||
if (typeof constraint.required === 'undefined') { | ||
constraint.required = true; | ||
} | ||
} else if (schema instanceof z.ZodEnum) { | ||
constraint.pattern = schema.options.map(option => // To escape unsafe characters on regex | ||
option.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')).join('|'); | ||
} | ||
if (typeof constraint.required === 'undefined') { | ||
constraint.required = true; | ||
return constraint; | ||
} | ||
return constraint; | ||
} | ||
var shape = getSchemaShape(source); | ||
var result = {}; | ||
function getSchemaShape(schema) { | ||
if (schema instanceof z.ZodObject) { | ||
return schema.shape; | ||
} else if (schema instanceof z.ZodEffects) { | ||
return getSchemaShape(schema.innerType()); | ||
} else if (schema instanceof z.ZodOptional) { | ||
return getSchemaShape(schema.unwrap()); | ||
if (shape) { | ||
for (var [key, def] of Object.entries(shape)) { | ||
// @ts-expect-error | ||
result[key] = inferConstraint(def); | ||
} | ||
} | ||
return null; | ||
return result; | ||
} | ||
function getError(error, scope) { | ||
var _error$errors$reduce; | ||
function resolve(source) { | ||
return { | ||
source, | ||
constraint: new Proxy({}, { | ||
get(_target, key) { | ||
if (typeof key !== 'string') { | ||
return; | ||
} | ||
return (_error$errors$reduce = error === null || error === void 0 ? void 0 : error.errors.reduce((result, e) => { | ||
var name = getName(e.path); | ||
var shape = getSchemaShape(source); | ||
var schema = shape === null || shape === void 0 ? void 0 : shape[key]; | ||
if (!schema) { | ||
return {}; | ||
} | ||
return inferConstraint(schema); | ||
} | ||
}), | ||
validate(form, submitter) { | ||
var payload = getFormData(form, submitter); | ||
var submission = createSubmission(payload); | ||
var value = cleanup(submission.form.value); | ||
var result = source.safeParse(value); | ||
var errors = !result.success ? result.error.errors.map(e => [getName(e.path), e.message]) : []; | ||
setFormError(form, errors); | ||
}, | ||
parse(payload) { | ||
var submission = createSubmission(payload); | ||
if (submission.state !== 'accepted') { | ||
return submission; | ||
} | ||
var value = cleanup(submission.form.value); | ||
var result = source.safeParse(value); | ||
if (result.success) { | ||
return { | ||
state: 'accepted', | ||
data: result.data, | ||
form: submission.form | ||
}; | ||
} else { | ||
return { | ||
state: 'rejected', | ||
form: { | ||
// @ts-expect-error | ||
value: submission.form.value, | ||
error: formatError(result.error) | ||
} | ||
}; | ||
} | ||
if (!scope || scope.includes(name)) { | ||
result.push([name, e.message]); | ||
} | ||
}; | ||
return result; | ||
}, [])) !== null && _error$errors$reduce !== void 0 ? _error$errors$reduce : []; | ||
} | ||
@@ -195,2 +129,2 @@ function ifNonEmptyString(fn) { | ||
export { ifNonEmptyString, resolve }; | ||
export { getError, getFieldsetConstraint, ifNonEmptyString }; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.3.1", | ||
"version": "0.4.0-pre.0", | ||
"main": "index.js", | ||
@@ -18,3 +18,3 @@ "module": "module/index.js", | ||
"peerDependencies": { | ||
"@conform-to/dom": "0.3.1", | ||
"@conform-to/dom": "0.4.0-pre.0", | ||
"zod": "^3.0.0" | ||
@@ -21,0 +21,0 @@ }, |
16139
312