Skip to content

Instantly share code, notes, and snippets.

@gregsantos
Last active September 18, 2022 19:15
Show Gist options
  • Save gregsantos/82a6bdfb18e2ef17010aac27eeff62d4 to your computer and use it in GitHub Desktop.
Save gregsantos/82a6bdfb18e2ef17010aac27eeff62d4 to your computer and use it in GitHub Desktop.
obj-validation-shallow
const isFn = v => typeof v === "function"
const isString = v => typeof v === "string"
function meta(meta) {
return pipe([
ix => {
ix.meta = meta
return Ok(ix)
},
])
}
///
JSON.stringify(obj1) === JSON.stringify(obj2)
// The ORDER of the properties IS IMPORTANT
///
validKeys = ["title", "description", "price", "image"]
userInput = { title: "My Title", Price: "8", c: 3, d: 4, e: 5 }
Object.keys(userInput).length > 4
Object.keys(userInput).forEach(
key => validKeys.includes(key) || delete userInput[key]
)
let validObj = ["title", "description", "price", "image"]
let validate = (obj, props) => props.every(prop => obj.hasOwnProperty(prop))
// FUNCTIONAL
function returnNewObjectOnlyValidKeys(obj, validKeys) {
const newObject = {}
Object.keys(obj).forEach(key => {
if (validKeys.includes(key)) newObject[key] = obj[key]
})
return newObject
}
// now you can use
;() => {
if (validate(obj, validObj)) obj => console.log(obj)
}
/// schema style validation
const schema = {
title: value => /^([A-Z][a-z\-]* )+[A-Z][a-z\-]*( \w+\.?)?$/.test(value),
price: value => parseInt(value) === Number(value) && value >= 18,
image: value => /^(\+?\d{1,2}-)?\d{3}-\d{3}-\d{4}$/.test(value),
}
schema.title.required = false
schema.price.required = false
schema.image.required = false
let info = {
// name: 'John Doe',
age: "",
phone: "123-456-7890",
}
const validate = (object, schema) =>
Object.entries(schema)
.map(([key, validate]) => [
key,
!validate.required || key in object,
validate(object[key]),
])
.filter(([_, ...tests]) => !tests.every(Boolean))
.map(
([key, invalid]) =>
new Error(`${key} is ${invalid ? "invalid" : "required"}.`)
)
const errors = validate(info, schema)
if (errors.length > 0) {
for (const { message } of errors) {
console.log(message)
}
} else {
console.log("info is valid")
}
///
function areEqualShallow(a, b) {
for (var key in a) {
if (!(key in b) || a[key] !== b[key]) {
return false
}
}
for (var key in b) {
if (!(key in a) || a[key] !== b[key]) {
return false
}
}
return true
}
function shallowEqual(object1, object2) {
const keys1 = Object.keys(object1)
const keys2 = Object.keys(object2)
if (keys1.length !== keys2.length) {
return false
}
for (let key of keys1) {
if (object1[key] !== object2[key]) {
return false
}
}
return true
}
/// DEEP EQUAL
function deepEqual(object1, object2) {
const keys1 = Object.keys(object1)
const keys2 = Object.keys(object2)
if (keys1.length !== keys2.length) {
return false
}
for (const key of keys1) {
const val1 = object1[key]
const val2 = object2[key]
const areObjects = isObject(val1) && isObject(val2)
if (
(areObjects && !deepEqual(val1, val2)) ||
(!areObjects && val1 !== val2)
) {
return false
}
}
return true
}
function isObject(object) {
return object != null && typeof object === "object"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment