
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.
@dandi/model
Advanced tools
`@dandi/model` provides decorators for defining models and validation metadata.
@dandi/model provides decorators for defining models and validation
metadata.
@dandi/model does not have any dependencies on NodeJS, and therefore
can be used on classes shared with projects targeted for the web.
The type of a model member is defined using the @Property() decorator:
import { Property } from '@dandi/model'
class MyModel {
@Property(String)
public name: string
}
Even with TypeScript, since there is no type data at runtime, you must provide a class in order to define a type. While it may be possible to use post processing using the generated AST to determine type, it is not implemented at this time.
In general, the type of the member should match the type specified in
@Property() decorator.
Models can have any number of levels of hierarchy, so long as the class
for child models is referenced in the @Property decorator:
import { Property } from '@dandi/model'
class ParentModel {
@Property(ChildModel)
public child: ChildModel
}
class ChildModel {
@Property(String)
public name: string
}
When using the @Property() decorator for a native primitive type such
as string, number, or boolean, use the corresponding class:
import { Property } from '@dandi/model'
class MyModel {
@Property(String)
public stringProperty: string
@Property(Number)
public numberProperty: number
@Property(Boolean)
public booleanProperty: boolean
}
In these cases, the class version of the primitive type is only used to identify the type. Validators should set the property using the native primitive type rather than the class.
Array<T> properties must use the @ArrayOf() decorator to define the type
of the array. The @ArrayOf() decorator is used in place of
@Property():
import { ArrayOf } from '@dandi/model'
class MyModel {
@ArrayOf(String)
public allMyStrings: string[]
}
Set<T> properties must use the @SetOf() decorator to define the type
of items contained in the set. The @SetOf() decorator is used in place
of @Property():
import { SetOf } from '@dandi/model'
class MyModel {
@SetOf(String)
public setMeUp: Set<string>
}
Map<TKey, TValue> properties must use the @MapOf() decorator to define the type
of items contained in the set. The @MapOf() decorator is used in place
of @Property():
import { Uuid } from '@dandi/common'
import { MapOf } from '@dandi/model'
class MyModel {
@MapOf(Uuid, Number)
public hereIsAMap: Map<Uuid, Number>[]
}
Use the @Required() decorator to specify a member that must be present
in order to pass validation.
import { Property, Required } from '@dandi/model'
class MyModel {
@Property(String)
@Required()
public youNeedThis: string
}
Use the @MinLength() and @MaxLength() decorators to define length
requirements for a string or array. These decorators are valid on any
type that has a length property.
import { ArrayOf, MaxLength, MinLength, Property } from '@dandi/model'
class MyModel {
@Property(String)
@MinLength(20)
public longString: string
@Property(String)
@MaxLength(5)
public shortString: string
@ArrayOf(String)
@MinLength(1)
public gottaHaveOne: string[]
}
Use the @MinValue() and @MaxValue() decorators to define value
requirements for number values.
import { MaxValue, MinValue, Property } from '@dandi/model'
class MyModel {
@Property(Number)
@MinValue(5)
@MaxValue(25)
public llamaCount: string
}
Use the @OneOf() decorator in place of @Property() to allow a member
be validated as one of two or more types.
import { OneOf } from '@dandi/model'
class MyModel {
@OneOf(Number, String)
public key: number | string
}
When using @OneOf(), the validator will use the first type that
successfully validates. In the above example, the validator would first
attempt to convert the value to a number, falling back to a string
if it failed.
@Pattern() - accepts a RegExp pattern that must be matched@Email() - a shorthand decorator for @Property(String),
@Pattern() using a built-in email validation pattern,
@MinLength(6), and @MaxLength(254)
1@UrlProperty() - a shorthand decorator for @Property(Url), and
@Pattern() using a built-in url validation pattern@DateTimeFormat(pattern) - a shorthand decorator for
@Property(DateTime) that define the specified pattern to be used for
parsing the Luxon DateTime object.@UrlArray - similar to @UrlProperty(), except it uses
@ArrayOf(Url) instead of @Property(Url)1: https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address
FAQs
`@dandi/model` provides decorators for defining models and validation metadata.
We found that @dandi/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.