Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
frint-data-validation
Advanced tools
Reactive data modelling package for Frint
With npm:
$ npm install --save frint-data frint-data-validation
Via unpkg CDN:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.0/Rx.min.js"></script>
<script src="https://unpkg.com/frint-data@latest/dist/frint-data.min.js"></script>
<script src="https://unpkg.com/frint-data@latest/dist/frint-data-validation.min.js"></script>
<script>
// available as `window.FrintDataValidation`
</script>
Validation rules can be set in two ways using frint-data-validation
.
validate()
functionImagine we have a Post model with this schema:
import { Types, createModel } from 'frint-data';
const Post = createModel({
schema: {
title: Types.string,
},
});
We can now optionally add our validation rules as follows:
Post.validationRules = [
{
name: 'titleIsNotEmpty', // this can come handy later for grouping errors
message: 'Title must not be empty',
rule: function (model) {
// `model` is your Post model's instance
if (post.title.length === 0) {
// we will fail this validation check
return false;
}
// everything is fine
return true;
}
}
];
To get the output of validation errors:
import { validate } from 'frint-data-validation';
const post = new Post({ title: '' });
const validationErrors = validate(post);
Since our title
is empty here, the validationErrors
will return this array:
console.log(validationErrors);
// [
// {
// name: 'titleIsNotEmpty',
// message: 'Title must not be empty'
// }
// ]
If there were no errors, validationErrors
would be an empty array.
You can also choose not to define the validation rules statically, and pass them when calling validate()
function:
import { validate } from 'frint-data-validation';
const post = new Post({ title: '' });
const validationErrors = validate(post, {
rules: [
{
name: 'titleIsNotEmpty',
message: 'Title must not be empty',
rule: model => model.title.length > 0,
},
],
});
If you want to keep observing the model for new errors as it keeps changing:
import { validate$ } from 'frint-data-validation';
const validationErrors$ = validate$(post);
Now the validationErrors$
observable can be subscribed, and it will emit an array with this structure:
[
{
name: string,
message: string,
}
]
validate(model, options)
model
(Model
): Model instance from frint-data
options
(Object
)options.rules
(Array
): Validation rulesArray
of validation errors.
validate$(model, options)
Reactive version of validate
function.
Observable
of validation errors.
FAQs
Validation for Models in FrintJS
The npm package frint-data-validation receives a total of 2 weekly downloads. As such, frint-data-validation popularity was classified as not popular.
We found that frint-data-validation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.