Skip to content

Commit 853bbdd

Browse files
committed
init setup
0 parents  commit 853bbdd

15 files changed

+17726
-0
lines changed

.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": ["lodash"],
3+
"presets": [["@babel/preset-env", {"targets": {"node": "current"}}]]
4+
}

.eslintrc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": ["airbnb", "prettier"],
3+
"plugins": ["prettier"],
4+
"parser": "babel-eslint",
5+
"rules": {
6+
"prettier/prettier": ["error"],
7+
"no-template-curly-in-string": "off",
8+
"import/prefer-default-export": "off"
9+
}
10+
}

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
npm-debug.log
2+
node_modules
3+
**/.DS_Store
4+
*.DS_Store
5+
.DS_Store
6+
.vscode
7+
*.log
8+
dist
9+
coverage

.prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"bracketSpacing": false
4+
}

README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# json-yup
2+
A tiny utility for converting a [JSON schema](https://json-schema.org/) into [Yup](https://github.com/jquense/yup) schema.
3+
4+
## Setup
5+
```
6+
npm install -S json-yup
7+
```
8+
```
9+
import createValidationSchema from 'json-yup';
10+
```
11+
12+
## API
13+
14+
```javascript
15+
createValidationSchema(schema, options);
16+
```
17+
18+
### Arguments
19+
|Argument | Type | Description |
20+
|-------------- | --- |---|---|
21+
|schema | Object | A valid JSON schema conforming to [JSON schema](https://json-schema.org/) specifications. |
22+
| customValidationFields | Object | Custom Yup mappings for schema properties |
23+
24+
### Options
25+
26+
|Property | Type | Description |
27+
|-------------- | --- |---|---|
28+
|blackList | Array | A list of fields to omit from the schema. |
29+
|customValidationFields | Object | Custom Yup mappings for schema properties |
30+
|validationTypes| Object | Custom Yup mappings for schema types. |
31+
32+
### Returns
33+
Yup validation object
34+
35+
## Usage
36+
37+
```javascript
38+
// Valid JSON Schema
39+
const jsonSchema = {
40+
"type": "object",
41+
"required": [
42+
"first_name"
43+
],
44+
"properties": {
45+
"create_at": {
46+
"type": "string"
47+
},
48+
"first_name": {
49+
"type": "string"
50+
},
51+
"age": {
52+
"type": "number",
53+
"min": 1,
54+
"max": 200
55+
}
56+
}
57+
};
58+
59+
// Build Yup Schema
60+
const validationSchema = createValidationSchema(jsonSchema, {
61+
blackList: [
62+
'create_at'
63+
],
64+
validationTypes: {
65+
string: yup.string().nullable()
66+
},
67+
customValidationFields: {
68+
first_name: yup.string().test(
69+
'isWilly',
70+
'Oops! You\'re not Willy',
71+
value => value === 'Willy'
72+
)
73+
}
74+
});
75+
76+
// Check validity
77+
validationSchema.isValid({
78+
first_name: 'Willy',
79+
age: 24
80+
})
81+
82+
```

__mocks__/schemas/simple.mock.json

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
{
2+
"type": "object",
3+
"required": [
4+
"unit_amount",
5+
"unit_quantity",
6+
"stops"
7+
],
8+
"properties": {
9+
"create_at": {
10+
"type": "string"
11+
},
12+
"updated_at": {
13+
"type": "string"
14+
},
15+
"first_name": {
16+
"type": "string"
17+
},
18+
"code": {
19+
"type": "string",
20+
"minLength": 1,
21+
"maxLength": 10
22+
},
23+
"unit_amount": {
24+
"type": "number",
25+
"format": "float",
26+
"minimum": 1,
27+
"maximum": 10,
28+
"description": "Cost of one unit of the line item"
29+
},
30+
"unit_quantity": {
31+
"type": "integer",
32+
"format": "int32",
33+
"default": 1
34+
},
35+
"unit_amount_currency": {
36+
"type": "string",
37+
"default": "USD",
38+
"enum": [
39+
"CAD",
40+
"USD"
41+
]
42+
},
43+
"charge_code": {
44+
"type": "object",
45+
"description": "Standardized code for a type of line item charge",
46+
"properties": {
47+
"id": {
48+
"type": "integer",
49+
"format": "int32"
50+
},
51+
"code": {
52+
"type": "string"
53+
},
54+
"description": {
55+
"type": "string"
56+
}
57+
}
58+
},
59+
"accessorials": {
60+
"type": "array",
61+
"items": {
62+
"type": "object",
63+
"description": "Accessorial code, price, and description",
64+
"required": [
65+
"id",
66+
"is_charge_code",
67+
"price",
68+
"currency_unit"
69+
],
70+
"properties": {
71+
"id": {
72+
"type": "integer",
73+
"format": "int32"
74+
},
75+
"is_charge_code": {
76+
"type": "boolean"
77+
},
78+
"price": {
79+
"type": "number",
80+
"format": "float"
81+
},
82+
"currency_unit": {
83+
"type": "string"
84+
}
85+
}
86+
}
87+
},
88+
"stops": {
89+
"type": "array",
90+
"items": {
91+
"type": "object",
92+
"required": [
93+
"name",
94+
"address"
95+
],
96+
"properties": {
97+
"name": {
98+
"type": "string"
99+
},
100+
"address": {
101+
"type": "string"
102+
}
103+
}
104+
}
105+
},
106+
"company": {
107+
"type": "object",
108+
"required": [
109+
"brokerage"
110+
],
111+
"allOf": [
112+
{
113+
"type": "object",
114+
"required": [
115+
"name",
116+
"dba_name"
117+
],
118+
"properties": {
119+
"id": {
120+
"type": "string",
121+
"format": "uuid"
122+
},
123+
"dba_name": {
124+
"type": "string"
125+
},
126+
"name": {
127+
"type": "string"
128+
}
129+
}
130+
},
131+
{
132+
"type": "object",
133+
"required": [
134+
"carrier"
135+
],
136+
"properties": {
137+
"carrier": {
138+
"type": "string",
139+
"format": "uuid"
140+
}
141+
}
142+
}
143+
]
144+
}
145+
},
146+
"allOf": [
147+
{
148+
"type": "object",
149+
"required": [
150+
"fire"
151+
],
152+
"properties": {
153+
"fire": {
154+
"type": "string"
155+
}
156+
}
157+
},
158+
{
159+
"type": "object",
160+
"required": [
161+
"ice"
162+
],
163+
"properties": {
164+
"ice": {
165+
"type": "string",
166+
"format": "uuid"
167+
}
168+
}
169+
}
170+
]
171+
}

0 commit comments

Comments
 (0)