-
Notifications
You must be signed in to change notification settings - Fork 94
New rewritten version 1.0.0 #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@icebob great work! |
Yes, Moleculer v0.14 will use it. |
@icebob I need some time to review updated code and rewrite TypeScript definitions. It will take ~2-3 days. |
@akazakou Ok. I've tried to update & add changes but I can't check that it's correct. And as I mentioned, would be good to add a typescript test as well, but I've got error message when I imported the validator. |
…ion-for-jest Integrate Jest tests for TypeScript definitions
Some workaround for this: /// <reference path="../../index.d.ts" /> // here we make a reference to exists module definition
import ValidatorType, { RuleNumber, ValidationSchema } from 'fastest-validator'; // here we importing type definition of default export
const Validator: typeof ValidatorType = require('../../index'); // here we importing real Validator Constructor
describe('Typescript Definitions', () => {
it('should compile validator', async () => {
const v: ValidatorType = new Validator();
const compiled = v.compile({
id: { type: "number", min: 1, integer: true } as RuleNumber
} as ValidationSchema);
expect(compiled).toBeInstanceOf(Function);
const res = compiled({
id: 2
});
expect(res).toBe(true);
});
}); I'm investigating, why it stops working... |
Signed-off-by: Andrei Kazakou <a.v.kazakou@gmail.com>
Signed-off-by: Andrei Kazakou <a.v.kazakou@gmail.com>
Signed-off-by: Andrei Kazakou <a.v.kazakou@gmail.com>
Signed-off-by: Andrei Kazakou <a.v.kazakou@gmail.com>
Signed-off-by: Andrei Kazakou <a.v.kazakou@gmail.com>
Adopt exists unit tests for using in TypeScript
@icebob could you publish a version as next? |
You can try it with |
Great job @icebob |
It's work I'll test it, also may I update the #50 ? Another thing maybe it will be a nice to try to reduce the bundle size special for browser. |
The full library has been rewritten. It uses code generators in order to be much faster.
Breaking changes
This new version contains several breaking changes.
Rule logic changed
The rule codes have been rewritten to code generator functions. Therefore if you use custom validators, you should rewrite them after upgrading.
Convert values
The
number
,boolean
anddate
rules have aconvert: true
property. In the previous version it doesn't modify the value in the checked object, just converted the value to the rules. In the version 1.0 this property converts the values in the checked object, as well.New
Sanitizations
The sanitization function is implemented. There are several rules which contains sanitizers. Please note, the sanitizers change the original checked object values.
boolean
convert
number
convert
date
convert
string
trim
string
trimLeft
string
trimRight
string
lowercase
string
uppercase
string
localeLowercase
String.toLocaleLowerCase
.string
localeUppercase
String.toLocaleUpperCase
.string
padStart
string
padEnd
string
convert
email
normalize
forbidden
remove
object
strict: "remove"
*
default
null
orundefined
.Root element validation
Basically the validator expects that you want to validate a Javascript object. If you want others, you can define the root level schema, as well. In this case set the
$$root: true
property.Example to validate a
string
variable instead ofobject
Enhanced shorthand types
You can use string-based shorthand validation definitions in the schema with properties.
Other changes
New
equal
ruleIt checks the value equal (
==
) to a static value or another property. Thestrict
property uses===
to check values.Example with static value:
Example with other field:
properties
in object ruleYou can use the
properties
property besides theprops
property in the object rule.