ascertain 0.8.8 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  Create data ascertain
26
26
  ```typescript
27
- import ascertain, { optional, and, or, $keys, $values, Schema } from 'ascertain';
27
+ import ascertain, { optional, and, or, $keys, $values, Schema, as } from 'ascertain';
28
28
 
29
29
  // create data sample
30
30
  const data = {
@@ -47,6 +47,11 @@ const data = {
47
47
  keyTwo: 2,
48
48
  keyThree: 3,
49
49
  },
50
+ // fault tolernat type casting
51
+ parsedNumber: as.number('1'),
52
+ parsedBoolean: as.boolean('false'),
53
+ parsedArray: as.array('1,2,3,4,5', ','),
54
+ parsedJSON: as.json('{ "number": 1 }'),
50
55
  };
51
56
 
52
57
  // create data schema
@@ -71,6 +76,12 @@ const schema: Schema<typeof data> = {
71
76
  [$keys]: /^key[A-Z]/,
72
77
  [$values]: Number
73
78
  },
79
+ parsedNumber: Number,
80
+ parsedBoolean: Boolean,
81
+ parsedArray: [String],
82
+ parsedJSON: {
83
+ number: 1,
84
+ },
74
85
  };
75
86
 
76
87
  // validate
@@ -79,7 +90,7 @@ const validate = ascertain<typeof data>(schema, data, '[DATA]');
79
90
 
80
91
  ## License
81
92
  License [The MIT License](http://opensource.org/licenses/MIT)
82
- Copyright (c) 2021 Ivan Zakharchanka
93
+ Copyright (c) 2022 Ivan Zakharchanka
83
94
 
84
95
  [npm-url]: https://www.npmjs.com/package/ascertain
85
96
  [downloads-image]: https://img.shields.io/npm/dw/ascertain.svg?maxAge=43200
package/build/index.d.ts CHANGED
@@ -13,6 +13,15 @@ export function optional<T = any>(schema: Schema<T>): Schema<any>;
13
13
  export function and<T = any>(...schema: Schema<T>[]): Schema<any>;
14
14
  export function or<T = any>(...schemas: Schema<T>[]): Schema<any>;
15
15
 
16
+ interface As {
17
+ number: (value: string | undefined) => number,
18
+ boolean: (value: string | undefined) => boolean,
19
+ array: (value: string | undefined, delimiter: string | RegExp) => string[];
20
+ json: <T>(value: string | undefined) => T;
21
+ }
22
+
23
+ export const as: As;
24
+
16
25
  export interface Ascertain {
17
26
  (data: any): void;
18
27
  }
package/build/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.or = exports.optional = exports.default = exports.ascertain = exports.and = exports.$values = exports.$keys = void 0;
6
+ exports.or = exports.optional = exports.default = exports.ascertain = exports.as = exports.and = exports.$values = exports.$keys = void 0;
7
7
 
8
8
  function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
9
9
 
@@ -106,6 +106,26 @@ var $keys = Symbol.for('@@keys');
106
106
  exports.$keys = $keys;
107
107
  var $values = Symbol.for('@@values');
108
108
  exports.$values = $values;
109
+ var as = {
110
+ number: function number(value) {
111
+ var result = parseFloat(value);
112
+ return Number.isNaN(result) ? undefined : result;
113
+ },
114
+ boolean: function boolean(value) {
115
+ return value ? !/^(0|false)$/i.test(value) : !!value;
116
+ },
117
+ array: function array(value, delimiter) {
118
+ return value ? value.split(delimiter) : undefined;
119
+ },
120
+ json: function json(value) {
121
+ try {
122
+ return JSON.parse(value);
123
+ } catch (e) {
124
+ return undefined;
125
+ }
126
+ }
127
+ };
128
+ exports.as = as;
109
129
 
110
130
  function certain(target, schema, path, optional) {
111
131
  if (schema === null || typeof schema === 'undefined') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ascertain",
3
- "version": "0.8.8",
3
+ "version": "0.9.0",
4
4
  "description": "0-Deps, simple, fast, for browser and node js object schema validator",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -36,7 +36,7 @@
36
36
  "@babel/cli": "^7.17.6",
37
37
  "@babel/core": "^7.17.8",
38
38
  "@babel/preset-env": "^7.16.11",
39
- "babel-jest": "^27.5.1",
40
- "jest": "^27.5.1"
39
+ "babel-jest": "^29.0.1",
40
+ "jest": "^29.0.1"
41
41
  }
42
42
  }