1.4.51 • Published 7 months ago

@laoban/validation v1.4.51

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

This is library for single field validation.

This is a common requirement in my applications: objects are parsed from JSON and I want to make sure that they meet a contract

I want a list of all the error messages, and I would like it if the code reads declarative.

The return type of the validation is a string[] or a Promise<string[]>

Approach

We wrap the object under test with a validate object then we can do things like the following. The validate object accumulates issues internaly and you can call .issues to see those issues

function validateProjectDetails(v: Validate<ProjectDetails>) {
    return v.isString("name").//
        isString("description").//
        isString("template").//
        isObject("details", validateDetails)
}

function validateDetails(v: Validate<Details>) {
    return v.isBoolean("publish", 'Should the project be published').//
        isArrayofObjects('links', v => v).//
        optObject("extraDeps", v => v, 'These are added to package.json dependencies').//
        optObject("extraDevDeps", v => v, 'These are added to package.json devDependencies').//
        optObject("extraBins", v => v, 'These are added to package.json bin')
}

Extra goodness

When working with CLIs I often want to validate that directories and files exist.

function validateTemplateDirectory(context: string, c: Config, templateDir: string): Promise<string[]> {
    let dir = path.join(c.templateDir, templateDir);
    return Validate.validateDirectoryExists(context, dir).then(dirErrors => dirErrors.length === 0 ?
        Validate.validateFile(`package.json in template directory ${templateDir}`, path.join(dir, 'package.json'), validatePackageJson) :
        dirErrors)
}```
1.4.51

7 months ago

1.4.50

8 months ago

1.4.49

8 months ago

1.4.46

9 months ago

1.4.42

9 months ago

1.4.41

9 months ago

1.4.44

9 months ago

1.4.43

9 months ago

1.4.45

9 months ago

1.4.40

10 months ago

1.4.39

10 months ago

1.4.38

10 months ago

1.4.37

10 months ago

1.4.36

10 months ago

1.4.35

10 months ago

1.4.34

10 months ago

1.4.32

12 months ago

1.4.31

2 years ago

1.4.30

2 years ago

1.4.20

2 years ago

1.4.22

2 years ago

1.4.21

2 years ago

1.4.26

2 years ago

1.4.25

2 years ago

1.4.28

2 years ago

1.4.27

2 years ago

1.4.29

2 years ago

1.4.13

2 years ago

1.4.15

2 years ago

1.4.14

2 years ago

1.4.16

2 years ago

1.4.18

2 years ago

1.4.11

2 years ago

1.4.10

2 years ago

1.4.12

2 years ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.4.9

2 years ago

1.4.8

2 years ago

1.4.7

2 years ago

1.3.4

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.41

2 years ago

1.2.39

2 years ago

1.2.38

2 years ago

1.2.37

2 years ago

1.2.36

2 years ago

1.2.35

2 years ago

1.2.34

2 years ago

1.2.33

2 years ago

1.2.32

2 years ago

1.2.31

2 years ago

1.2.30

2 years ago

1.2.29

2 years ago

1.2.28

2 years ago

1.2.27

2 years ago

1.2.26

2 years ago

1.2.25

2 years ago

1.2.24

2 years ago

1.2.23

2 years ago

1.2.22

2 years ago

1.2.21

2 years ago

1.2.20

2 years ago

1.2.19

2 years ago

1.2.18

2 years ago

1.2.17

2 years ago

1.2.16

2 years ago

1.2.15

2 years ago

1.2.14

2 years ago

1.2.13

2 years ago

1.2.12

2 years ago

1.2.11

2 years ago

1.2.10

2 years ago

1.2.9

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.4.10

2 years ago

0.4.9

2 years ago

0.4.8

2 years ago

0.4.7

2 years ago

0.4.6

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago