
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.
convict-model
Advanced tools
Annotate a class to define and validate your configs using convict just like you do with an ORM. If you like annotating models classes, then this package will tickle your fancy. The code style and patterns are based on Typeorm because they know what's up. If your using a IOC/DI system, ConvictModel will fit in real nice.
| Contributing | Changelog | Convict |
|---|
npm install @akirix/convict-model --save
reflect-metadata if you have not already so the annotations work.npm install reflect-metadata --save
then import it in global scope aka main file, i.e. app.ts or index.ts
import "reflect-metadata";
npm install convict --save
npm install @types/convict --save-dev
tsconfig.json"emitDecoratorMetadata": true,
"experimentalDecorators": true,
npm install js-yaml --save
First we need a proper project setup like the one below with a folder to hold our config schema classes. This is a very simple Typescript folder structure.
MyProject
├── src // place of your TypeScript code
│ ├── schema // place where your config entities will go
│ │ ├── MyConfig.ts // sample entity
│ │ └── SubConfig.ts // a nested entity
│ ├── types.d.ts // place to put your interfaces
│ └── index.ts // start point of your application
├── .gitignore // standard gitignore file
├── config.json // Your apps config file
├── package.json // node module dependencies
├── README.md // a readme file
└── tsconfig.json // TypeScript compiler options
Take note of the src/schema directory, here we will put our config schema classes
which will be annotated with convict schema definitions. This directory can be called whatever
you like by the way. Technically you don't even need the folder, it's just a good idea
to put similar classes together for organization.
Now we can start building up a model for our config schema.
It's a good idea to define an interface so your experience can be agile and include all the fancy IDE features. Interfaces also open an opportunity to have more than one implementation of your config, i.e. maybe you use convict competitor or no validation on config at all.
src/types.d.ts
declare namespace config {
export interface MyConfig {
name: string;
subConfig: SubConfig;
}
export interface SubConfig {
bar: number;
}
}
Now we can define a schema class and decorate it like Christmas. The parameter for
@Property decorator is simply a convict SchemaObj like in normal convict. You can
read all about the possible options in convicts documentation.
src/schema/MyConfig.ts
import { Property } from '@akirix/convict-model';
import SubConfig from './SubConfig';
import * as yaml from 'js-yaml';
@Config({
as: 'foo',// an alias for the config file, i.e base name
dir: 'config',// relative to NODE_PATH or cwd()
parser: {
extension: ['yml', 'yaml'],
parse: yaml.safeLoad
}
})
export default class MyConfig implements config.MyConfig {
@Property({
doc: 'The name of the thing',
default: 'Convict',
env: 'MY_CONFIG_NAME'
})
public name: string;
@Property(SubConfig)
public subConfig: SubConfig;
}
src/schema/SubConfig.ts
export default class SubConfig {
@Property({
doc: 'A sub prop',
default: 3,
env: 'SUB_CONFIG_BAR',
format: 'int'
})
public bar: number;
}
Now we can make our configuration for our app. This can be a hardcoded Object in your code, a json file, a yaml file, or however you do it. In the end it's up to you how you type out and load the data.
config.json
{
"name": "Cool App",
"subConfig": {
"bar": 5
}
}
We have a couple of ways to load it up so you can choose what works for your unique situation. The example below is the simplest way in the spirit of TL;DR.
src/index.ts
import { getConvictModel, ConvictModel } from '@akirix/convict-model';
//get your config file however you do it
const myRawConfig = getMyConfigData();
//initialize the ConvictModel with a list of paths or entities to load as the schema
const convictModel: ConvictModel = getConvictModel(['src/schema/**/*.*s']);
//get your validated config object as a serialized class
const myConfig: config.MyConfig = convictModel.create<config.MyConfig>('MyConfig',myRawConfig);
FAQs
Model style decorators for your convict config.
We found that convict-model 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.