validate-value
Advanced tools
Comparing version 9.0.0-internal.1 to 9.0.0
@@ -0,1 +1,16 @@ | ||
# [9.0.0](https://github.com/thenativeweb/validate-value/compare/8.9.29...9.0.0) (2021-05-17) | ||
### Features | ||
* Parse, don't validate. ([#345](https://github.com/thenativeweb/validate-value/issues/345)) ([880db64](https://github.com/thenativeweb/validate-value/commit/880db642be3af1fdcdf03c38bbf2c1a3a2a796f1)) | ||
### BREAKING CHANGES | ||
* Parsing invalid data does not throw an exception anymore; Instead it returns an error-Result object. | ||
- Returns a Result object that either contains a parsed value or an error, instead of just throwing an error. This encourages parsing of data over validating it and provides assistance with TypeScript types of parsed variables. | ||
- Improves error messages slightly. | ||
## [8.9.29](https://github.com/thenativeweb/validate-value/compare/8.9.28...8.9.29) (2021-05-17) | ||
@@ -2,0 +17,0 @@ |
{ | ||
"name": "validate-value", | ||
"version": "9.0.0-internal.1", | ||
"version": "9.0.0", | ||
"description": "validate-value validates values against JSON schemas.", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
@@ -27,22 +27,22 @@ # validate-value | ||
const doThingsWithValidation = async function (options: unknown): Promise<void> { | ||
// This throws, if the options are not valid. | ||
validateOptions(options); | ||
// This throws, if the options are not valid. | ||
validateOptions(options); | ||
const typedOptions = options as Options; | ||
const typedOptions = options as Options; | ||
doSomethingWithAnOption(typedOptions.someOption); | ||
doSomethingWithAnOption(typedOptions.someOption); | ||
}; | ||
const doThingsWithParsing = async function (options: unknown): Promise<Result<void, Error>> { | ||
// This parses the options and unwraps them, throwing if they were invalid. | ||
// If invalid options were something our program could handle, we would not | ||
// want to throw here and instead handle the error appropriately. | ||
// In this example we want the program to crash, if the options are invalid. | ||
const typedOptions = parseOptions(options).unwrapOrThrow(); | ||
// This parses the options and unwraps them, throwing if they were invalid. | ||
// If invalid options were something our program could handle, we would not | ||
// want to throw here and instead handle the error appropriately. | ||
// In this example we want the function to throw, if the options are invalid. | ||
const typedOptions = parseOptions(options).unwrapOrThrow(); | ||
doSomethingWithAnOption(typedOptions.someOption); | ||
} | ||
doSomethingWithAnOption(typedOptions.someOption); | ||
}; | ||
``` | ||
In the second example, the typedOptions contained in the `Result` returned from the `parseOptions` call already have the type that we expect them to have and we don't have to assert them or assign them to a new variable in any way. This combines better support from the TypeScript compiler with better error handling from [`defekt`](https://github.com/thenativeweb/defekt/). | ||
In the second example, the `typedOptions` contained in the `Result` returned from the `parseOptions` call already have the type that we expect them to have and we don't have to assert them or assign them to a new variable in any way. This combines better support from the TypeScript compiler with better error handling from [`defekt`](https://github.com/thenativeweb/defekt/). | ||
@@ -77,3 +77,3 @@ ## Quick start | ||
If you are using typescript, you will want to provide a type for the parsed value: | ||
If you are using TypeScript, you will want to provide a type for the parsed value: | ||
@@ -109,3 +109,3 @@ ```typescript | ||
After parsing, `parsedValue` will have the type `User`, since it was passed to the Parser upon construction. | ||
After parsing, `parsedValue` will have the type `User`, since it was passed to the parser upon construction. | ||
@@ -127,3 +127,3 @@ ## Configuring the parser | ||
For convenience, there is also the `parse` function, which skips the creation of a parser instance. You can use this if you're only going to use a schema for validation once. Otherwise, it is recommended to first create a parser instance, since then the json schema is only compiled once. | ||
For convenience, there is also the `parse` function, which skips the creation of a parser instance. You can use this if you're only going to use a schema for validation once. Otherwise, it is recommended to first create a parser instance, since then the JSON schema is only compiled once: | ||
@@ -141,3 +141,3 @@ ```javascript | ||
required: [ 'username', 'password' ] | ||
}) | ||
}); | ||
``` | ||
@@ -147,3 +147,3 @@ | ||
To verify that a variable is of a specific type, use the `isOfType` function. Hand over a value you would like to verify, and a JSON schema describing that type. The function returns `true` if the given variable matches the schema, and `false` if it doesn't: | ||
To verify that a variable is of a specific type, use the `isOfType` function. Hand over the value you would like to verify, and a JSON schema describing that type. The function returns `true` if the given variable matches the schema, and `false` if it doesn't: | ||
@@ -173,3 +173,3 @@ ```javascript | ||
When using TypeScript, you may even specify a generic type parameter, and use the function as a type guard. | ||
When using TypeScript, you may even specify a generic type parameter, and use the function as a type guard: | ||
@@ -176,0 +176,0 @@ ```typescript |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
0
40526
25