
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
schema-to-types
Advanced tools
Library to generate TypeScript types based on simpl-schema definitions.
Library to generate TypeScript types based on simpl-schema definitions.
Define schemas once, get both validation and type checking!
npm install --save-dev schema-to-types
Define all your schemas as values of a single object with type SchemaMap and export it.
The keys of the object will be used as name of the generated types.
Example:
export const schemas: SchemaMap = {};
// Will generate interface Foo
schemas["Foo"] = new SimpleSchema({
anArrayOfBooleans: {
type: Array
},
"anArrayOfBooleans.$": {
type: Boolean
},
aDate: {
type: Date,
optional: true,
autoValue: () => new Date()
},
aString: {
type: String,
defaultValue: "schemas"
}
});
Will generate:
export interface Foo {
anArrayOfBooleans: boolean[];
aDate?: Date;
aString: string;
}
Very basic for the moment: run update-model.ts script,
providing path to tsconfig and the file that will contain the generated types.
Example:
node node_modules/schema-to-types/dist/src/update-model.js tsconfig.json api/imports/model/schema-model.ts
To reference another schema, simply reference it via its key in the schemas object.
schemas["SubType"] = new SimpleSchema({
aNumber: {
type: Number,
min: 12,
max: 14.5
}
});
schemas["Foo"] = new SimpleSchema({
aSubSchemaExternal: schemas["SubType"], // Will target the other type
});
Will generate:
export interface SubType {
aNumber: number;
}
export interface Foo {
aSubObject: SubType;
}
When an enum is used as a type in your schema, it will be "deconstructed" as the list of possible values.
Except... if you provide the specific typeName attribute.
For this to be accepted by Simple-schema, you will need to allow it:
SimpleSchema.extendOptions(["typeName"]);
Example
import { SchemaMap } from "../src/schema-map";
import { MyEnum } from "./models/model";
// Essential to be allowed to use typeName property and reference types explicitly
SimpleSchema.extendOptions(["typeName"]);
schemas["TypeWithEnumus"] = new SimpleSchema({
anEnum: {
type: MyEnum, // Will generate the list of possible values
optional: true
},
anEnumWithType: {
type: MyEnum,
typeName: 'MyEnum' // Will reference MyEnum type directly
}
});
To reference a type instead of the native types (such as String):
typeName property (see above)type property to make sure the type is imported in the file (mandatory for the code generation to reference it)Example
Let's assume an Id type has been defined in model/model.ts:
export type Id = string;
In the schema file:
import { SchemaMap, allowSchemaExtension } from "../src/schema-map";
import { Id } from "./models/model";
// Essential to be allowed to use typeName property and reference types explicitly
SimpleSchema.extendOptions(["typeName"]);
schemas["TypeWithTypedString"] = new SimpleSchema({
aTypedString: {
type: String as (value?: any) => Id, // To make sure Id is imported
typeName: "Id"
},
});
This way the library will use Id instead of string in the generated interface.
To test the tool, look at /example dir and run
npm run generate-test-model
typeName for external referencesSimpleSchema.extendOptions(['typeName']); automatically?FAQs
Library to generate TypeScript types based on simpl-schema definitions.
We found that schema-to-types 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.