Skip to content

Commit a79e34a

Browse files
committed
allow geo information fields
1 parent 93c325a commit a79e34a

4 files changed

Lines changed: 155 additions & 23 deletions

File tree

src/components/ga4/EventBuilder/ValidateEvent/schemas/baseContent.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,16 @@ describe("baseContentSchema", () => {
142142

143143
expect(validator.isValid(validInput)).toEqual(false)
144144
})
145+
146+
describe("with ip_override", () => {
147+
test("is valid with a valid IPv4 address", () => {
148+
const validInput = {
149+
events: [{ name: "something", params: {} }],
150+
ip_override: "127.0.0.1",
151+
}
152+
const validator = new Validator(baseContentSchema)
153+
expect(validator.isValid(validInput)).toEqual(true)
154+
})
155+
})
156+
145157
})

src/components/ga4/EventBuilder/ValidateEvent/schemas/baseContent.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,34 @@
22

33
import { userPropertiesSchema } from './userProperties'
44
import { eventsSchema } from './events'
5+
import { userLocationSchema } from "./userLocation"
56

67
export const baseContentSchema = {
7-
"type": "object",
8-
"required": ["events"],
9-
"additionalProperties": false,
10-
"properties": {
11-
"app_instance_id": {
12-
"type": "string",
13-
"format": "app_instance_id"
14-
},
15-
"client_id": {
16-
"type": "string",
17-
},
18-
"user_id": {
19-
"type": "string"
20-
},
21-
"timestamp_micros": {
22-
// "type": "number"
23-
},
24-
"user_properties": userPropertiesSchema,
25-
"non_personalized_ads": {
26-
"type": "boolean"
27-
},
28-
"events": eventsSchema,
29-
}
8+
type: "object",
9+
required: ["events"],
10+
additionalProperties: false,
11+
properties: {
12+
app_instance_id: {
13+
type: "string",
14+
format: "app_instance_id",
15+
},
16+
client_id: {
17+
type: "string",
18+
},
19+
user_id: {
20+
type: "string",
21+
},
22+
timestamp_micros: {
23+
// "type": "number"
24+
},
25+
user_properties: userPropertiesSchema,
26+
non_personalized_ads: {
27+
type: "boolean",
28+
},
29+
events: eventsSchema,
30+
user_location: userLocationSchema,
31+
ip_override: {
32+
type: "string",
33+
},
34+
},
3035
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import "jest"
2+
import { Validator } from "../validator"
3+
import { userLocationSchema } from "./userLocation"
4+
5+
describe("userLocationSchema", () => {
6+
test("can be used to validate a valid payload", () => {
7+
const validInput = {
8+
city: "Mountain View",
9+
region_id: "US-CA",
10+
country_id: "US",
11+
subcontinent_id: "021",
12+
continent_id: "019",
13+
}
14+
const validator = new Validator(userLocationSchema)
15+
expect(validator.isValid(validInput)).toEqual(true)
16+
})
17+
18+
test.each([["US-CA"], ["US-C"], ["US-C1A"]])(
19+
"is valid with a valid region_id: %s",
20+
region_id => {
21+
const validator = new Validator(userLocationSchema)
22+
expect(validator.isValid({ region_id })).toEqual(true)
23+
}
24+
)
25+
26+
test.each([["USA-CA"], ["US-CALI"], ["us-ca"], ["US_CA"]])(
27+
"is invalid with an invalid region_id: %s",
28+
region_id => {
29+
const validator = new Validator(userLocationSchema)
30+
expect(validator.isValid({ region_id })).toEqual(false)
31+
}
32+
)
33+
34+
test("is valid with a valid country_id", () => {
35+
const validInput = { country_id: "US" }
36+
const validator = new Validator(userLocationSchema)
37+
expect(validator.isValid(validInput)).toEqual(true)
38+
})
39+
40+
test.each([["USA"], ["U"], ["us"]])(
41+
"is invalid with an invalid country_id: %s",
42+
country_id => {
43+
const validator = new Validator(userLocationSchema)
44+
expect(validator.isValid({ country_id })).toEqual(false)
45+
}
46+
)
47+
48+
test("is valid with a valid subcontinent_id", () => {
49+
const validInput = { subcontinent_id: "021" }
50+
const validator = new Validator(userLocationSchema)
51+
expect(validator.isValid(validInput)).toEqual(true)
52+
})
53+
54+
test.each([["21"], ["0211"], ["abc"]])(
55+
"is invalid with an invalid subcontinent_id: %s",
56+
subcontinent_id => {
57+
const validator = new Validator(userLocationSchema)
58+
expect(validator.isValid({ subcontinent_id })).toEqual(false)
59+
}
60+
)
61+
62+
test("is valid with a valid continent_id", () => {
63+
const validInput = { continent_id: "019" }
64+
const validator = new Validator(userLocationSchema)
65+
expect(validator.isValid(validInput)).toEqual(true)
66+
})
67+
68+
test.each([["19"], ["0199"], ["abc"]])(
69+
"is invalid with an invalid continent_id: %s",
70+
continent_id => {
71+
const validator = new Validator(userLocationSchema)
72+
expect(validator.isValid({ continent_id })).toEqual(false)
73+
}
74+
)
75+
76+
test("is invalid with additional properties", () => {
77+
const invalidInput = {
78+
city: "Mountain View",
79+
extra_prop: "should fail",
80+
}
81+
const validator = new Validator(userLocationSchema)
82+
expect(validator.isValid(invalidInput)).toEqual(false)
83+
})
84+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// User Location Schema
2+
3+
export const userLocationSchema = {
4+
type: "object",
5+
additionalProperties: false,
6+
properties: {
7+
city: {
8+
type: "string",
9+
},
10+
region_id: {
11+
type: "string",
12+
// ISO 3166-2
13+
pattern: "^[A-Z]{2}-[A-Z0-9]{1,3}$",
14+
},
15+
country_id: {
16+
type: "string",
17+
// ISO 3166-1 alpha-2
18+
pattern: "^[A-Z]{2}$",
19+
},
20+
subcontinent_id: {
21+
type: "string",
22+
// UN M49
23+
pattern: "^[0-9]{3}$",
24+
},
25+
continent_id: {
26+
type: "string",
27+
// UN M49
28+
pattern: "^[0-9]{3}$",
29+
},
30+
},
31+
}

0 commit comments

Comments
 (0)