Skip to content

Commit 5e57ada

Browse files
committed
Start removing ow validation
1 parent 7b2ebe3 commit 5e57ada

File tree

6 files changed

+369
-232
lines changed

6 files changed

+369
-232
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"bignumber.js": "^9.0.0",
3030
"bnc-sdk": "0.1.1",
3131
"bowser": "^2.5.2",
32-
"ow": "^0.13.2",
3332
"promise-cancelable": "^2.1.1",
3433
"regenerator-runtime": "^0.13.3",
3534
"svelte": "^3.0.0",

rollup.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export default [
3939
external: [
4040
"bowser",
4141
"bnc-sdk",
42-
"ow",
4342
"bignumber.js",
4443
"svelte-i18n",
4544
"svelte",

src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ import {
1515
walletInterface
1616
} from "./stores"
1717

18-
import { validateInit, validateConfig } from "./validation"
1918
import { isMobileDevice } from "./utilities"
2019
import { initializeBlocknative } from "./services"
2120

2221
import { version } from "../package.json"
2322

2423
function init(initialization) {
25-
validateInit(initialization)
24+
// validateInit(initialization)
2625

2726
const { subscriptions, dappId, networkId, modules } = initialization
2827

@@ -107,7 +106,7 @@ function init(initialization) {
107106
}
108107

109108
function config(options) {
110-
validateConfig(options)
109+
// validateConfig(options)
111110
app.update(store => ({ ...store, ...options }))
112111
}
113112

src/stores.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { writable, derived } from "svelte/store"
22
import Cancelable from "promise-cancelable"
3-
import { validateWalletInterface } from "./validation"
43
import { getBlocknative } from "./services"
54
import { wait, makeQuerablePromise } from "./utilities"
65

@@ -75,7 +74,7 @@ function createWalletInterfaceStore(initialState) {
7574
subscribe,
7675
update,
7776
set: walletInterface => {
78-
validateWalletInterface(walletInterface)
77+
// validateWalletInterface(walletInterface)
7978
set(walletInterface)
8079
}
8180
}

src/validation.js

Lines changed: 199 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,204 @@
1-
import ow from "ow"
2-
31
export function validateInit(init) {
4-
ow(
5-
init,
6-
"Initialization Options",
7-
ow.object.exactShape({
8-
networkId: ow.number,
9-
dappId: ow.string,
10-
subscriptions: ow.optional.object.exactShape({
11-
address: ow.optional.function,
12-
network: ow.optional.function,
13-
balance: ow.optional.function,
14-
wallet: ow.optional.function
15-
}),
16-
modules: ow.object.exactShape({
17-
walletSelect: ow.object.exactShape({
18-
heading: ow.string,
19-
description: ow.string,
20-
wallets: ow.object.exactShape({
21-
mobile: ow.optional.array.nonEmpty.ofType(
22-
ow.object.exactShape({
23-
name: ow.string,
24-
iconSrc: ow.optional.string,
25-
iconSrcSet: ow.optional.string,
26-
svg: ow.optional.string,
27-
wallet: ow.function,
28-
link: ow.optional.string,
29-
installMessage: ow.optional.function,
30-
preferred: ow.optional.boolean
31-
})
32-
),
33-
desktop: ow.optional.array.nonEmpty.ofType(
34-
ow.object.exactShape({
35-
name: ow.string,
36-
iconSrc: ow.optional.string,
37-
iconSrcSet: ow.optional.string,
38-
svg: ow.optional.string,
39-
wallet: ow.function,
40-
link: ow.optional.string,
41-
installMessage: ow.optional.function,
42-
preferred: ow.optional.boolean
43-
})
44-
)
45-
})
46-
}),
47-
walletReady: ow.array.nonEmpty.ofType(ow.function)
48-
})
2+
if (!init || typeof init !== "object") {
3+
throw new Error(
4+
`"onboard" must be called with an initialization object, received: ${init}`
5+
)
6+
}
7+
8+
const { networkId, dappId, subscriptions, modules } = init
9+
10+
if (!dappId || typeof dappId !== "string") {
11+
throw new Error(`"dappId" must be of type string, received: ${dappId}`)
12+
}
13+
14+
if (!networkId || typeof networkId !== "number") {
15+
throw new Error(
16+
`"networkId" must be of type number, received: ${networkId}`
17+
)
18+
}
19+
20+
validateSubscriptions(subscriptions)
21+
validateModules(modules)
22+
}
23+
24+
function validateSubscriptions(subscriptions) {
25+
if (subscriptions) {
26+
if (typeof subscriptions !== "object") {
27+
throw new Error(
28+
`"subscriptions" must be of type object, received: ${subscriptions}`
29+
)
30+
}
31+
32+
Object.keys(subscriptions).forEach(key => {
33+
if (!validSubscriptionKey(subscriptions[key])) {
34+
throw new Error(
35+
`${key} is not a valid subscription listener, received: ${key}`
36+
)
37+
}
38+
39+
if (subscriptions[key] !== "function") {
40+
throw new Error(
41+
`subscription listener must be a function, received: ${subscriptions[key]}`
42+
)
43+
}
4944
})
50-
)
45+
}
46+
}
47+
48+
function validateModules(modules) {
49+
if (!modules || typeof modules !== "object") {
50+
throw new Error(
51+
`"onboard" must be called with a modules object, received: ${modules}`
52+
)
53+
}
54+
55+
const { walletSelect, walletReady } = modules
56+
57+
validateWalletSelect(walletSelect)
58+
validateWalletReady(walletReady)
59+
}
60+
61+
function validateWalletSelect(walletSelect) {
62+
if (!walletSelect || typeof walletSelect !== "object") {
63+
throw new Error(
64+
`"modules" must have a "walletSelect" object, received: ${walletSelect}`
65+
)
66+
}
67+
68+
const { heading, description, wallets } = walletSelect
69+
70+
if (!heading || typeof heading !== "string") {
71+
throw new Error(
72+
`"walletSelect" requires a "heading" parameter of type string, received: ${heading}`
73+
)
74+
}
75+
76+
if (!description || typeof description !== "string") {
77+
throw new Error(
78+
`"walletSelect" requires a "description" parameter of type string, received: ${description}`
79+
)
80+
}
81+
82+
if (!wallets || typeof wallets !== "object") {
83+
throw new Error(
84+
`"walletSelect" must have a "wallets" object, received: ${wallets}`
85+
)
86+
}
87+
88+
const { mobile, desktop } = wallets
89+
90+
if (mobile) {
91+
if (!Array.isArray(mobile) || mobile.length < 1) {
92+
throw new Error(
93+
`"mobile" must be an array of at least one wallet object, received: ${mobile}`
94+
)
95+
}
96+
97+
mobile.forEach(module => {
98+
validateWalletModule(module)
99+
})
100+
}
101+
102+
if (desktop) {
103+
if (!Array.isArray(desktop) || desktop.length < 1) {
104+
throw new Error(
105+
`"desktop" must be an array of at least one wallet object, received: ${desktop}`
106+
)
107+
}
108+
109+
desktop.forEach(module => {
110+
validateWalletModule(module)
111+
})
112+
}
113+
}
114+
115+
function validateWalletModule(module) {
116+
const {
117+
name,
118+
iconSrc,
119+
iconSrcSet,
120+
svg,
121+
wallet,
122+
link,
123+
installMessage,
124+
preferred
125+
} = module
126+
127+
if (!name || typeof name !== "string") {
128+
throw new Error(
129+
`wallet module must have a name property of type string, received: ${name}`
130+
)
131+
}
132+
133+
if (!wallet || typeof wallet !== "string") {
134+
throw new Error(
135+
`wallet module must have a "wallet" property of type string, received: ${wallet}`
136+
)
137+
}
138+
139+
if (iconSrc && typeof iconSrc !== "string") {
140+
throw new Error(`"iconSrc" must be of type string, received: ${iconSrc}`)
141+
}
142+
143+
if (iconSrcSet && typeof iconSrcSet !== "string") {
144+
throw new Error(
145+
`"iconSrcSet" must be of type string, received: ${iconSrcSet}`
146+
)
147+
}
148+
149+
if (svg && typeof svg !== "string") {
150+
throw new Error(`"svg" must be of type string, received: ${svg}`)
151+
}
152+
153+
if (link && typeof link !== "string") {
154+
throw new Error(`"link" must be of type string, received: ${link}`)
155+
}
156+
157+
if (installMessage && typeof installMessage !== "function") {
158+
throw new Error(
159+
`"installMessage" must be of type function, received: ${installMessage}`
160+
)
161+
}
162+
163+
if (preferred && typeof preferred !== "boolean") {
164+
throw new Error(
165+
`"preferred" must be of type boolean, received: ${preferred}`
166+
)
167+
}
168+
}
169+
170+
function validateWalletReady(walletReady) {
171+
if (!walletReady || typeof walletReady !== "object") {
172+
throw new Error(
173+
`"modules" must have a "walletReady" object, received: ${walletReady}`
174+
)
175+
}
176+
177+
if (!Array.isArray(walletReady) || walletReady.length < 1) {
178+
throw new Error(
179+
`"walletReady" must be an array of at least one readiness object, received: ${walletReady}`
180+
)
181+
}
182+
183+
walletReady.forEach(module => {
184+
if (typeof module !== "function") {
185+
throw new Error(
186+
`"walletReady" module must be of type function, received: ${module}`
187+
)
188+
}
189+
})
190+
}
191+
192+
function validSubscriptionKey(key) {
193+
switch (key) {
194+
case "address":
195+
case "network":
196+
case "balance":
197+
case "wallet":
198+
return true
199+
default:
200+
return false
201+
}
51202
}
52203

53204
export function validateConfig(configuration) {

0 commit comments

Comments
 (0)