Gathering detailed insights and metrics for @teamteanpm2024/ab-deserunt-culpa
Gathering detailed insights and metrics for @teamteanpm2024/ab-deserunt-culpa
Gathering detailed insights and metrics for @teamteanpm2024/ab-deserunt-culpa
Gathering detailed insights and metrics for @teamteanpm2024/ab-deserunt-culpa
npm install @teamteanpm2024/ab-deserunt-culpa
Typescript
Module System
Node Version
NPM Version
50
Supply Chain
32.1
Quality
80.3
Maintenance
100
Vulnerability
100
License
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-25%
3
Compared to previous week
Last month
-11.8%
15
Compared to previous month
Last year
0%
505
Compared to previous year
37
Generates Typescript/Javascript decoders source code expressions from JSON schema specifications.
You can also try it in your browser with any JSON schema.
This package aims to provide the best possible conversion of a JSON Schema type into it's equivalent decoder function.
It has a wide support of different features from different JSON Schema drafts, check the Support Matrix for more details.
To use the CLI you can install the package globally:
1npm install -g @teamteanpm2024/ab-deserunt-culpa
To use it as a library in your package, install it locally:
1npm install --dev @teamteanpm2024/ab-deserunt-culpa 2# or 3yarn add -D @teamteanpm2024/ab-deserunt-culpa
To convert a JSON definition file to decoders, use the following command CLI:
@teamteanpm2024/ab-deserunt-culpa <schema.json>
The package also exports an API that be used for more elaborate integrations:
1import Converter from "@teamteanpm2024/ab-deserunt-culpa"; 2 3console.log(await Converter.convertFile("path/to/schema.json"));
There are a few functios you can use for invoking the converter:
1// Synchronous variations
2convertSchema(schema: Schema, options?: ConverterOptions): string;
3convertContents(buffer: string, options?: ConverterOptions): string;
4
5// Asynchronous variations
6convertFile(file: string, options?: ConverterOptions): Promise<string>;
7convert(url: string | Schema, options?: ConverterOptions): Promise<string>;
The ConverterOptions
have the following properties:
nsPrefix
: An optional namespace where to look for decoder functions into.
For example, if you are importing the decoders like so:
1import * as D from "decoders";
Then you can use:
1const code = convertSchema(schema, {
2 nsPrefix: "D.",
3});
nsLib
: An optional namespace where to look for extra decoder library functions exposed by the @teamteanpm2024/ab-deserunt-culpa
package.
If not specified, all of the avanced decoders would be disabled.
For example, if you can import the utility library like so:
1import * as L from "@teamteanpm2024/ab-deserunt-culpa/decoders";
Then you can use:
1const code = convertSchema(schema, {
2 nsLib: "L.",
3});
resolveRefPointer
: An optional function to call when a $ref is encountered. The returned value will replace the contents of that ref.
For example, given the following logic:
1const schema = { 2 type: "array", 3 items: { 4 $ref: "#/components/schema/Item", 5 }, 6}; 7const code = convertSchema(schema, { 8 resolveRefPointer: (expr) => { 9 return expr.split("/").pop(); 10 }, 11});
Will produce the following code:
1array(Item);
resolveRefSchema
: An optional function to call when a $ref is encountered and the schema of it is required.
In contrast to resolveRefPointer
, where a variable reference is emitted, this function expects the value of the referred schema to be returned.
If missing, resolveRefPointer
would be used when possible, and if not, an exception would be thrown.
The following table summarizes the supported conversion between JSON Schema types and decoders.
Type | Validation / Keyword | Status |
---|---|---|
All Types | enum | ✅ |
const | ✅ | |
References | Basic Support | ✅ [1] |
any | Basic Support | ✅ |
string | Basic Support | ✅ |
minLength | ✅ | |
maxLength | ✅ | |
pattern | ✅ | |
format: "date-time | ✅ | |
format: "time | - | |
format: "date | - | |
format: "duration | - | |
format: "email | ✅ | |
format: "idn-email | - | |
format: "hostname | ✅ | |
format: "idn-hostname | - | |
format: "ipv4 | - | |
format: "ipv6 | - | |
format: "uuid | ✅ | |
format: "uri | ✅ | |
format: "uri-reference | - | |
format: "iri | - | |
format: "iri-reference | - | |
integer | Basic Support | ✅ |
number | Basic Support | ✅ |
multipleOf | ✅ | |
minimum | ✅ | |
maximum | ✅ | |
exclusiveMinimum | ✅ | |
exclusiveMaximum | ✅ | |
boolean | Basic Support | ✅ |
null | Basic Support | ✅ |
array | Basic Support | ✅ |
Unevaluated Items | - | |
items | ✅ | |
prefixItems | 🟨 [2] | |
contains | - | |
minContains | - | |
maxContains | - | |
minItems | ✅ | |
maxItems | ✅ | |
uniqueItems | ✅ | |
object | Basic Support | ✅ [3] |
Unevaluated Properties | - | |
Extending Closed Schemas | - | |
properties | ✅ | |
additionalProperties | ✅ | |
required | ✅ | |
patternProperties | ✅ | |
propertyNames | ✅ | |
minProperties | ✅ | |
maxProperties | ✅ | |
Schema Composition | allOf | ✅ |
oneOf | 🟨 [4] | |
anyOf | ✅ | |
discriminator | ✅ | |
Conditional Schemas | dependentRequired | - |
dependentSchemas | - | |
if | - | |
then | - | |
else | - |
Remarks:
[1] Implemented through a user-provided reference resolution function that returns the variable name of a previously defined decoder.
[2] Currently
prefixItems
cannot be used together withitems
. This means that declaring additional items for arrays is not supported.
[3] Note that while for
type: "object"
the JSON Schema spec indicates that "Using non-strings as keys is invalid JSON", the javascript implementation implicitly converts all properties to strings, so the decoders will always validate even numbers in the object keys.
[4] The
oneOf
is currently polyfilled with theanyOf
behaviour. This means that the "exactly one" validation is not respected.
No vulnerabilities found.
No security vulnerabilities found.