Comparing version 1.0.0-alpha.1 to 1.0.0-alpha.2
@@ -8,6 +8,3 @@ export declare abstract class Type<T> { | ||
nullable(): NullableType<this>; | ||
try(value: unknown): { | ||
error: ValidationError | null; | ||
value: T | null; | ||
}; | ||
try(value: unknown): T | ValidationError; | ||
} | ||
@@ -14,0 +11,0 @@ export declare class ValidationError extends Error { |
@@ -16,7 +16,6 @@ "use strict"; | ||
try { | ||
const result = this.parse(value); | ||
return { value: result, error: null }; | ||
return this.parse(value); | ||
} | ||
catch (err) { | ||
return { error: err, value: null }; | ||
return err; | ||
} | ||
@@ -23,0 +22,0 @@ } |
{ | ||
"name": "myzod", | ||
"version": "1.0.0-alpha.1", | ||
"version": "1.0.0-alpha.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./libs/index.js", |
@@ -116,8 +116,12 @@ # myzod | ||
Takes an unknown value and returns a result object with value/error properties. This api if you do not want to throw exceptions. | ||
Takes an unknown value and returns a result which will either be the parsed value or an instance of ValidationError. | ||
This api is useful if you do not want to throw exceptions. | ||
```typescript | ||
// Error will be a ValidationError on failure and null on success. | ||
// Value will be the parsed value on success and null on failure | ||
const { value, error } = schema.try(data); | ||
const result = schema.try(data); | ||
if (result instanceof myzod.ValidationError) { | ||
// handle Error | ||
} else { | ||
// result is of type: myzod.Infer<typeof schema> | ||
} | ||
``` | ||
@@ -124,0 +128,0 @@ |
641
66676
1159