Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@jaysalvat/smart-model
Advanced tools
________ _____ ______ ________ ________ _________ _____ ______ ________ ________ _______ ___
|\ ____\|\ _ \ _ \|\ __ \|\ __ \|\___ ___\\ _ \ _ \|\ __ \|\ ___ \|\ ___ \ |\ \
\ \ \___|\ \ \\\__\ \ \ \ \|\ \ \ \|\ \|___ \ \_\ \ \\\__\ \ \ \ \|\ \ \ \_|\ \ \ __/|\ \ \
\ \_____ \ \ \\|__| \ \ \ __ \ \ _ _\ \ \ \ \ \ \\|__| \ \ \ \\\ \ \ \ \\ \ \ \_|/_\ \ \
\|____|\ \ \ \ \ \ \ \ \ \ \ \ \\ \| \ \ \ \ \ \ \ \ \ \ \\\ \ \ \_\\ \ \ \_|\ \ \ \____
____\_\ \ \__\ \ \__\ \__\ \__\ \__\\ _\ \ \__\ \ \__\ \ \__\ \_______\ \_______\ \_______\ \_______\
|\_________\|__| \|__|\|__|\|__|\|__|\|__| \|__| \|__| \|__|\|_______|\|_______|\|_______|\|_______|
\|_________|
Javascript object model.
Works on modern browsers. Check if tests pass on your browser.
Provided formats are IFFE, ESM, CJS and UMD, all minified.
npm install @jaysalvat/smart-model
import SmartModel from '@jaysalvat/smart-model'
import SmartModel from 'https://unpkg.com/@jaysalvat/smart-model@latest/build/smart-model.esm.min.js'
<script src="https://unpkg.com/@jaysalvat/smart-model@latest/build/smart-model.min.js"></script>
Better documentation soon...
function readingtime(text) { /*...*/ }
const Post = SmartModel.create('Post', {
title: {
required: true,
type: String,
},
body: {
required: true,
type: String,
rule: {
'tooShort': (calue) => value.length < 100,
'tooLong': (calue) => value.length > 1000,
}
},
createdAt: {
type: Date,
default: new Date(),
transform: (value) => new Date(value),
format: (value) => value.toLocaleString()
},
updatedAt: {
type: Date,
default: new Date(),
transform: (value) => new Date(value),
format: (value) => value.toLocaleString()
},
// Nested model
author: {
required: true,
type: {
firstname: {
required: true,
type: String
},
lastname: {
type: String
}
}
},
// Virtual properties
bodyLength: (post) => post.body.length,
readingTime: (post) => readingTime(post.body)
},
// Settings
{
strict: false,
exceptions: true
}
// Events
{
onUpdate() {
this.updatedAt = new Date()
}
}
)
const post = new Post({
title: 'My new post',
body: 'Lorem ipsum...',
author: {
firstname: 'Brad',
lastname: 'Pitt'
}
})
Options for property:
Option | Type | Description |
---|---|---|
type | any | The required type () of a value. You can set a schema or another model () in order to nest models |
required | bool | The value is required. See settings.empty for the empty check function |
readonly | bool | The value can't be overwritten |
default | any | The default value if the property is undefined |
transform | fn | A function to transform the value to set |
format | fn | A function to format the value to get |
rule | object | An object which contains the validation rules (**) |
Type can be String
, Boolean
, Number
, Date
, Function
or a class.
If a schema is set as a type, a nested model will be created.
const Post = SmartModel.create('Post', {
title: {
type: String
},
body: {
type: String
},
Date: {
type: Date
},
author: {
type {
firstname: {
type: String
},
lastnaame: {
type: String
}
}
}
})
An existing Model can be set as a type in order to nest this Model.
const Author = SmartModel.create('Author', {
firstname: {
type: String
},
lastnaame: {
type: String
}
})
const Post = SmartModel.create('Post', {
title: {
type: String
},
body: {
type: String
},
Date: {
type: Date
},
body: {
type: string
},
author: {
type: Author
}
})
Multiple rules of validation can be set on a property.
const Discount = SmartModel.create('Discount', {
percent: {
type: Number,
rule: {
'min': {value) => value < 0,
'max': {value) => value > 100
}
}
})
Option | Type | Default | Description |
---|---|---|---|
strict | bool | false | Allow to set property not present in the schema |
empty | fn | fn (***) | Function to check if a value is empty if required |
exceptions | bool/object | object (****) | Throw exceptions on errors. can be boolean or òbject` for advanced settings |
The default function to check if a value is empty is:
(value) => value === '' || value === null || value === undefined
Option | Type | Default |
---|---|---|
readonly | bool | false |
required | bool | true |
rule | bool | true |
strict | bool | false |
type | bool | true |
const article = new Article()
article.put({
title: 'My article',
})
Same as $put, but only passed property are updated.
const article = new Article()
const json = article.eject()
Methods can be added to models.
const Article = SmartModel.create('Article', {
body: {
type: String
}
}, {}, {
excerpt(limit = 10) {
return this.body.substr(0, limit) + '…'
}
})
Models have some callbacks methods that are called when properties are set, get, updated or deleted.
const User = SmartModel.create('User', {
username: {
type: String
}
}, {}, {
$onBeforeGet() {}
$onBeforeSet() {}
$onBeforeUpdate() {}
$onDelete() {}
$onGet() {}
$onBeforeDelete() {}
$onSet() {}
$onUpdate() {}
})
Dev mode
npm run dev
Build
npm run build
Lint
npm run lint
Fix lint errors
npm run lint:fix
Tests
npm run test
npm run test:watch
npm run test:browser
Bump version and publish to NPM
npm run release
npm run release:patch
npm run release:minor
npm run release:major
FAQs
SmartModel is a fun experiment over Javascript Proxy. It tends to bring useful tools and best practices to data objects.
The npm package @jaysalvat/smart-model receives a total of 3 weekly downloads. As such, @jaysalvat/smart-model popularity was classified as not popular.
We found that @jaysalvat/smart-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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.