Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
as-typed
Type magic to convert a JSON Schema literal into the proper TypeScript type representation, all without additional build steps. This module has no runtime functionality by itself. It exposes a single AsTyped
type which takes a valid JSON Schema and outputs the equivalent type for it. With this you can get type safety at runtime and validate your values at runtime writing types just once. Great for JSON integrations and data serialization.
This is forked from https://github.com/wix-incubator/as-typed fixing many bugs, modernizing and introducing support for more types thanks to newer TypeScript features.
npm install --save-dev as-typed
import { AsTyped } from "as-typed";
const schema = {
title: "Example Schema",
type: "object",
required: ["firstName", "age", "hairColor"],
properties: {
firstName: {
type: "string",
},
lastName: {
type: "string",
},
age: {
type: "integer",
minimum: 0,
},
hairColor: {
enum: ["black", "brown", "blue"],
type: "string",
},
},
} as const; // <<< "as const" is important to preserve literal type
type SchemaT = AsTyped<typeof schema>;
/*
type SchemaT = {
firstName: string;
age: number;
hairColor: "black" | "brown" | "blue";
lastName?: string | undefined;
};
*/
AsTyped<{type: "string"}>
=== string
AsTyped<{type: "number"}>
=== number
AsTyped<{type: "integer"}>
=== number
AsTyped<{type: "boolean"}>
=== boolean
AsTyped<{type: "null"}>
=== null
AsTyped<{type: "undefined"}>
=== undefined
Patterns are not supported. There is no regex validation in typescript Typescript issue 6579
Value validation (min, max etc) is not supported Typescript is not meant for value checking (at least currently).
AsTyped<{type: "object", properties: {foo: {type: "number"}}>
=== {foo?: number}
AsTyped<{type: "object", properties: {foo: {type: "number"}, bar: {type: "string}, required: ["foo"]}>
=== {foo: number, bar?: string}
AsTyped<{type: "array", items: [{type: "number"}, {type: "string"}], additionalItems: {type: "boolean"}}>
=== [number, string, ...boolean[]]
AsTyped<{type: "array", items: {type: "array", items: {type: "string"}}}>
=== string[][]
AsTyped<{type: "object", properties: {arr: {type: "array", items: {type: "object", additionalProperties: {type: "string"}}}}}
=== {arr?: {[name: string]: string}[]}
AsTyped<{type: "array", items: {type: "string"}}>
=== string[]
AsTyped<{type: "array", items: [{type: "string"}, {type: "number"}]}>
=== [string, number]
AsTyped<{type: "array", items: [{type: "string"}, {type: "number"}], additionalItems: {type: "string"}}}>
=== [string, number, ...string[]]
AsTyped<{definitions: {foo: {$id: "foo", type: "number"}}, $ref: "foo"}>
=== number
AsTyped<{definitions: {str1: {$id: "str1", $ref: "str2"}, str2: {$id: "str2", type: "string"}}, $ref: "str1"}>
=== string
AsTyped<{definitions: {foo: {type: "number"}}, $ref: "#/definitions/foo"}>
=== number
Not
works mainly on primitive types, e.g. AsTyped<{not: {type: "string"}}>
will resolve to number | object | any[] | boolean | null | undefined
AsTyped<{oneOf: [{type: "string"}, {type: "number"}]}>
=== string | number
Currently doesn"t work as expected, and resolves the same as anyOf. See Typescript issue 20863
AsTyped<{allOf: [{type: "object", properties: {a: {type: "number"}}}, {type: "object", properties: {b: {type: "string"}}}]}>
=== {a?: number, b?: string}
AsTyped<{allOf: [{type: "object", properties: {a: {type: "number"}}}, {type: "object", properties: {b: {type: "string"}}, required: ["b"]}]}>
=== {a?: number, b: string} | {a?: number} | {b: string}
If/Then/Else
acts exactly like {oneOf: [{allOf: [If, Then]}, Else]}
. It's strange to have this sugar in the schema which doesn't reduce the verbosity.
Currently doesn't work as expected, for the same reasons as oneOf. Resolves to (If & Then) | Else
, which is not an accurate translation. See Typescript issue 20863
FAQs
Static TypeScript types from a literal JSONSchema type
The npm package as-typed receives a total of 10,568 weekly downloads. As such, as-typed popularity was classified as popular.
We found that as-typed demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.