@scalar/validation 0.3.0 → 0.3.2
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/CHANGELOG.md +12 -0
- package/package.json +6 -2
- package/.turbo/turbo-build.log +0 -4
- package/src/coerce.test.ts +0 -1158
- package/src/coerce.ts +0 -224
- package/src/helpers/is-object.test.ts +0 -168
- package/src/helpers/is-object.ts +0 -31
- package/src/index.ts +0 -37
- package/src/schema.ts +0 -250
- package/src/typegen.test.ts +0 -214
- package/src/typegen.ts +0 -268
- package/src/types.ts +0 -110
- package/src/validate.test.ts +0 -833
- package/src/validate.ts +0 -106
- package/tsconfig.build.json +0 -11
- package/tsconfig.json +0 -10
- package/vite.config.ts +0 -13
package/src/validate.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { isObject } from './helpers/is-object'
|
|
2
|
-
import type { Schema } from './schema'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Validates that a given value matches the specified schema.
|
|
6
|
-
*
|
|
7
|
-
* The schema describes the expected structure/type of data.
|
|
8
|
-
* Supported schema types include:
|
|
9
|
-
* - 'any': Accepts any value.
|
|
10
|
-
* - 'number': Only numbers are valid.
|
|
11
|
-
* - 'string': Only strings are valid.
|
|
12
|
-
* - 'boolean': Only booleans are valid.
|
|
13
|
-
* - 'nullable': Only `null` is valid.
|
|
14
|
-
* - 'notDefined': Only `undefined` is valid.
|
|
15
|
-
* - 'array': Array with all items validated recursively.
|
|
16
|
-
* - 'record': Object with string/number keys and values, checked recursively.
|
|
17
|
-
* - 'object': Plain object with fixed property keys, each validated recursively.
|
|
18
|
-
* - 'union': Accepts if value matches any of the listed schemas.
|
|
19
|
-
* - 'optional': Accepts `undefined` or a value matching the inner schema.
|
|
20
|
-
* - 'intersection': Accepts if value matches every member schema (members are object schemas; value must be a plain object).
|
|
21
|
-
* - 'literal': Exact match with a literal value.
|
|
22
|
-
* - 'lazy': Delegates to the schema returned by the factory.
|
|
23
|
-
* - 'evaluate': Transforms value then validates against an inner schema.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* import { number, object, string, validate } from '@scalar/validation'
|
|
28
|
-
*
|
|
29
|
-
* const schema = object({ id: number(), name: string() })
|
|
30
|
-
* validate(schema, { id: 1, name: 'Ada' }) // true
|
|
31
|
-
* validate(schema, { id: 1, name: 2 }) // false
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* If schema is `undefined`, validation fails.
|
|
35
|
-
* Returns true if the value matches the schema, false otherwise.
|
|
36
|
-
*/
|
|
37
|
-
export const validate = (schema: Schema | undefined, value: unknown): boolean => {
|
|
38
|
-
if (!schema) {
|
|
39
|
-
return false
|
|
40
|
-
}
|
|
41
|
-
if (schema.type === 'any') {
|
|
42
|
-
return true
|
|
43
|
-
}
|
|
44
|
-
if (schema.type === 'number') {
|
|
45
|
-
return typeof value === 'number' && !Number.isNaN(value) && Number.isFinite(value)
|
|
46
|
-
}
|
|
47
|
-
if (schema.type === 'string') {
|
|
48
|
-
return typeof value === 'string'
|
|
49
|
-
}
|
|
50
|
-
if (schema.type === 'boolean') {
|
|
51
|
-
return typeof value === 'boolean'
|
|
52
|
-
}
|
|
53
|
-
if (schema.type === 'nullable') {
|
|
54
|
-
return value === null
|
|
55
|
-
}
|
|
56
|
-
if (schema.type === 'notDefined') {
|
|
57
|
-
return value === undefined
|
|
58
|
-
}
|
|
59
|
-
if (schema.type === 'array') {
|
|
60
|
-
return Array.isArray(value) && value.every((item) => validate(schema.items, item))
|
|
61
|
-
}
|
|
62
|
-
if (schema.type === 'record') {
|
|
63
|
-
if (!isObject(value)) {
|
|
64
|
-
return false
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const keys = Object.keys(value)
|
|
68
|
-
return keys.every((key) => validate(schema.key, key) && validate(schema.value, value[key]))
|
|
69
|
-
}
|
|
70
|
-
if (schema.type === 'object') {
|
|
71
|
-
if (!isObject(value)) {
|
|
72
|
-
return false
|
|
73
|
-
}
|
|
74
|
-
const schemaKeys = Object.keys(schema.properties)
|
|
75
|
-
return schemaKeys.every((key) => validate(schema.properties[key], value[key]))
|
|
76
|
-
}
|
|
77
|
-
if (schema.type === 'optional') {
|
|
78
|
-
return value === undefined || validate(schema.schema, value)
|
|
79
|
-
}
|
|
80
|
-
if (schema.type === 'union') {
|
|
81
|
-
return schema.schemas.some((schema) => validate(schema, value))
|
|
82
|
-
}
|
|
83
|
-
if (schema.type === 'intersection') {
|
|
84
|
-
if (schema.schemas.length === 0) {
|
|
85
|
-
// Vacuous: no constraints (matches `Array.prototype.every` on an empty list).
|
|
86
|
-
return true
|
|
87
|
-
}
|
|
88
|
-
if (!isObject(value)) {
|
|
89
|
-
return false
|
|
90
|
-
}
|
|
91
|
-
return schema.schemas.every((subSchema) => validate(subSchema, value))
|
|
92
|
-
}
|
|
93
|
-
if (schema.type === 'literal') {
|
|
94
|
-
return value === schema.value
|
|
95
|
-
}
|
|
96
|
-
if (schema.type === 'lazy') {
|
|
97
|
-
return validate(schema.schema(), value)
|
|
98
|
-
}
|
|
99
|
-
if (schema.type === 'evaluate') {
|
|
100
|
-
return validate(schema.schema, schema.expression(value))
|
|
101
|
-
}
|
|
102
|
-
// We need to assert here that schema has the type never so we know we handle all cases
|
|
103
|
-
const _exhaustive: never = schema
|
|
104
|
-
console.warn('Unknown schema type:', _exhaustive)
|
|
105
|
-
return false
|
|
106
|
-
}
|
package/tsconfig.build.json
DELETED
package/tsconfig.json
DELETED
package/vite.config.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { resolve } from 'node:path'
|
|
2
|
-
|
|
3
|
-
import { defineConfig } from 'vite'
|
|
4
|
-
|
|
5
|
-
export default defineConfig({
|
|
6
|
-
plugins: [],
|
|
7
|
-
resolve: {
|
|
8
|
-
alias: {
|
|
9
|
-
'@': resolve(import.meta.dirname, './src'),
|
|
10
|
-
'@test': resolve(import.meta.dirname, './test'),
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
})
|