Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@darch/schema
Advanced tools
TypeScript schema declaration and validation library with static type inference
Typescript schema validation with static type inference.
Schemas are a crucial part of a microservices architecture or a detachable application architecture (darch). They can serve as contracts between different pieces of an application (frontend, backend, forms) and different services. So schemas should be easily portable, written and read, and that's the goal of this package.
To install:
npm install @darch/schema
⚠️ IMPORTANT: You must enable strict
mode in your tsconfig.json
. This is a best practice for all TypeScript projects.
// tsconfig.json
{
// ...
"compilerOptions": {
// ...
"strict": true
}
}
const addressSchema = createSchema({
street: 'string',
number: 'int?',
});
const schemaDefinition = {
name: 'string', // any string
email: 'email?', // email type - will validate against email regex
age: 'int?', // optional integer
notes: '[int]?',
// declaring an union
unionField: [['string?', '[int]?']],
// represents an enum
letter: ['a', 'b', 'c'],
// more detailed way to define enums
letterOptionalList: {
enum: ['x', 'y', 'z'],
optional: true,
list: true,
},
// using a previous schema as field type
optionalAddress: {
type: addressSchema,
optional: true,
},
// another way to define schema fields
deliveryAddress: {
schema: {
street: 'string',
number: 'int?',
},
},
} as const; // "as const" is needed to TS to infer types correctly
const userSchema = createSchema(schemaDefinition);
expect(() => userSchema.parse({ name: 'Antonio', letter: 'x' })).toThrow(
`field "letter": accepted: 'a' or 'b' or 'c', found x.`
);
expect(() => userSchema.parse({ name: 'antonio', letter: 'a', deliveryAddress: {} })).toThrow(
'field "deliveryAddress": ➤ field "street": expected type string, found undefined.'
);
const parsed = userSchema.parse({ name: 'antonio', letter: 'a', deliveryAddress: { street: 'alameda' } });
type InferType = typeof parsed;
assert<
IsExact<
InferType,
{
name: string;
email?: string | undefined;
age?: number | undefined;
notes?: number[] | undefined;
unionField?: string | number[] | undefined;
letter: 'a' | 'b' | 'c';
letterOptionalList?: ('x' | 'y' | 'z')[] | undefined;
optionalAddress?:
| {
street: string;
number?: number | undefined;
}
| undefined;
deliveryAddress: {
street: string;
number?: number | undefined;
};
}
>
>(true);
Returns a string of an interface representing a DarchSchema;
import { schemaToTypescript } from '@darch/schema/lib/schemaToTypescript';
const interfaceTxt = await schemaToTypescript('User', userSchema);
const interfaceTxt = await schemaToTypescript('User', userSchema);
expect(interfaceTxt).toBe(`
export interface User {
name: string;
email?: Email;
age?: number;
notes?: number[];
unionField?: string | number[];
letter: "a" | "b" | "c";
letterOptionalList?: ("x" | "y" | "z")[];
optionalAddress?: {
street: string;
number?: number;
};
deliveryAddress: {
street: string;
number?: number;
};
}`);
Receives a DarchSchema and returns a json-schema
import { schemaToJSON } from '@darch/schema/lib/schemaToJSON';
const jsonSchema = schemaToJSON('User', userSchema);
expect(jsonSchema).toEqual({
properties: {
address: {
properties: {
number: {
type: 'integer',
},
street: {
type: 'string',
},
},
required: ['street'],
title: '',
type: 'object',
},
age: {
type: 'integer',
},
email: {
tsType: 'Email',
type: 'string',
},
letter: {
enum: ['a', 'b', 'c'],
title: 'EnumLetterUser',
type: 'string',
},
letterOptionalList: {
items: {
enum: ['x', 'y', 'z'],
title: 'Enum__subLetterOptionalList',
type: 'string',
},
type: 'array',
},
name: {
type: 'string',
},
notes: {
items: {
type: 'integer',
},
type: 'array',
},
},
required: ['name', 'letter'],
title: 'User',
type: 'object',
});
FAQs
TypeScript schema declaration and validation library with static type inference
The npm package @darch/schema receives a total of 201 weekly downloads. As such, @darch/schema popularity was classified as not popular.
We found that @darch/schema 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.