项目作者: mattlean

项目描述 :
Data validation & transformation library
高级语言: TypeScript
项目地址: git://github.com/mattlean/checkpointjs.git
创建时间: 2019-02-09T05:18:26Z
项目社区:https://github.com/mattlean/checkpointjs

开源协议:MIT License

下载


THIS LIBRARY IS CURRENTLY UNSTABLE! USE AT YOUR OWN RISK!

Checkpoint.js

Validate and transform data.

Setup

Download

Install the checkpointjs package with a package manager like npm or Yarn.

You can also download and extract a release from here from the Checkpoint.js GitHub repository releases page.

Using the Library

The library can be used in two different ways:

Direct Function Import

  1. import { validate } from 'checkpointjs'
  2. const data = {
  3. foo: 'bar',
  4. 123: 456
  5. }
  6. const result = validate(data, {
  7. schema: {
  8. foo: { isRequired: true, type: 'string' },
  9. 123: { type: 'number' }
  10. },
  11. type: 'object'
  12. })

Checkpoint Instantiation

  1. import checkpoint from 'checkpointjs'
  2. const data = {
  3. foo: 'bar',
  4. 123: 456
  5. }
  6. const result = checkpoint(data).validate({
  7. schema: {
  8. foo: { isRequired: true, type: 'string' },
  9. 123: { type: 'number' }
  10. },
  11. type: 'object'
  12. })

Note: This library supports TypeScript. The source is completely written in it. Declaration files are included in the dist/ folder.

API

Validate

Validates the input data. Returns the results of the validation.

Functions

  1. // Direct Function Import
  2. validate(data, rules)
  3. // Checkpoint Instantiation
  4. checkpoint(data).validate(rules)

Result

TODO

Schema

allowNull
  • Description: Determines if a null value is allowed.
  • Type: boolean
  • Default: false
  1. // Primitive
  2. const primitiveData = null
  3. const primitiveValidationResult = validate(primitiveData, {
  4. schema: { allowNull: true },
  5. type: 'primitive'
  6. })
  7. console.log(primitiveValidationResult.pass) // true
  8. // Object
  9. const objectData = { foo: null }
  10. const objectValidationResult = validate(objectData, {
  11. schema: {
  12. foo: { allowNull: true }
  13. },
  14. type: 'object'
  15. })
  16. console.log(objectValidationResult.pass) // true
  17. // Array of primitives
  18. const primitiveArrayData = [null, null]
  19. const primitiveArrayValidationResult = validate(primitiveArrayData, {
  20. schema: { allowNull: true },
  21. type: 'array',
  22. arrayType: 'primitive'
  23. })
  24. console.log(primitiveArrayValidationResult.pass) // true
  25. // Array of objects
  26. const objectArrayData = [
  27. { foo: null },
  28. { foo: null }
  29. ]
  30. const objectArrayValidationResult = validate(objectArrayData, {
  31. schema: { allowNull: true },
  32. type: 'array',
  33. arrayType: 'object'
  34. })
  35. console.log(objectArrayValidationResult.pass) // true
isRequired
  • Description: Determines if the value is required.
  • Type: boolean
  • Default: false
  1. // Primitive
  2. const primitiveData = 123
  3. const primitiveValidationResult = validate(primitiveData, {
  4. schema: { isRequired: true },
  5. type: 'primitive'
  6. })
  7. console.log(primitiveValidationResult.pass) // true
  8. // Object
  9. const objectData = { foo: 123 }
  10. const objectValidationResult = validate(objectData, {
  11. schema: {
  12. foo: { isRequired: true }
  13. },
  14. type: 'object'
  15. })
  16. console.log(objectValidationResult.pass) // true
  17. // Array of primitives
  18. const primitiveArrayData = [123, 456]
  19. const primitiveArrayValidationResult = validate(primitiveArrayData, {
  20. schema: { isRequired: true },
  21. type: 'array',
  22. arrayType: 'primitive'
  23. })
  24. console.log(primitiveArrayValidationResult.pass) // true
  25. // Array of objects
  26. const objectArrayData = [
  27. { foo: 123 },
  28. { foo: 456 }
  29. ]
  30. const objectArrayValidationResult = validate(objectArrayData, {
  31. schema: { isRequired: true },
  32. type: 'array',
  33. arrayType: 'object'
  34. })
  35. console.log(objectArrayValidationResult.pass) // true
stringValidation

TODO

type
  • Description: Requires a matching type for the value.
  • Type: string
  1. // Primitive
  2. const primitiveData = 'foo'
  3. const primitiveValidationResult = validate(primitiveData, {
  4. schema: { type: 'string' },
  5. type: 'primitive'
  6. })
  7. console.log(primitiveValidationResult.pass) // true
  8. // Object
  9. const objectData = { foo: 123 }
  10. const objectValidationResult = validate(objectData, {
  11. schema: {
  12. foo: { type: 'number' }
  13. },
  14. type: 'object'
  15. })
  16. console.log(objectValidationResult.pass) // true
  17. // Array of primitives
  18. const primitiveArrayData = [true, false]
  19. const primitiveArrayValidationResult = validate(primitiveArrayData, {
  20. schema: { type: 'boolean' },
  21. type: 'array',
  22. arrayType: 'primitive'
  23. })
  24. console.log(primitiveArrayValidationResult.pass) // true
  25. // Array of objects
  26. const objectArrayData = [
  27. { foo: 'bar' },
  28. { foo: 'baz' }
  29. ]
  30. const objectArrayValidationResult = validate(objectArrayData, {
  31. schema: { type: 'string' },
  32. type: 'array',
  33. arrayType: 'object'
  34. })
  35. console.log(objectArrayValidationResult.pass) // true

Options

exitASAP

TODO

requireMode

TODO

Transform

Transforms and mutates the input data. Returns the transformed data.

Functions

  1. // Direct Function Import
  2. transform(data, commands)
  3. // Checkpoint Instantiation
  4. checkpoint(data).transform(commands)

Commands

clean
  • Description: Removes undefined values.
  1. // Object
  2. const objectData = { a: 123, b: undefined, c: 456, d: 789, e: undefined }
  3. transform(objectData, 'clean')
  4. console.log(objectData) // { a: 123, c: 456, d: 789 }
replace
  • Description: Replaces values with another value.
  1. // Object
  2. const objectData = { a: 123, b: 456, c: 789 }
  3. transform(objectData, { name: 'replace', options: [456, 'xyz'] })
  4. console.log(objectData) // { a: 123, c: 'xyz', d: 789 }
trim
  • Description: Trims whitespace from strings.
  1. // Object
  2. const objectData = { a: 'hey ', b: ' ho', c: ' let\'s go ' }
  3. transform(objectData, 'trim')
  4. console.log(objectData) // { a: 'hey', c: 'ho', d: 'let\'s go' }

Checkpoint

TODO

License

This open source project is licensed under the MIT License.